[back]

Take While

Implement the function

val take_while : ('a -> bool) -> 'a list -> 'a list

so that take_while f l is the result of taking the longest prefix from l whose elements satisfy the predicate f.

Examples

let _ = assert (take_while (fun x -> x <= 3) [1;2;3;4;5;4;3;2;1] = [1;2;3])
let _ = assert (take_while (fun x -> x) [true;true;false;true] = [true;true])
let _ = assert (take_while (fun _ -> true) [1;2;3;4;5] = [1;2;3;4;5])
let _ = assert (take_while (fun _ -> false) [1;2;3;4;5] = [])