- Using
List.fold_left
, write a functionfor_all : ('a -> bool) -> 'a list -> bool
. It takes as argument a listl
of type'a list
, and a predicatep
of type'a -> bool
. It must returntrue
if and only if all elements ofl
satisfy the predicatep
. - Using
List.fold_left
, write a functionexists : ('a -> bool) -> 'a list -> bool
. It takes as argument a listl
of type'a list
, and a predicatep
of type'a -> bool
. It must returnstrue
if at least one element ofl
satisfies the predicatep
. - Write a function
sorted : ('a -> 'a -> int) -> 'a list -> bool
, usingList.fold_left
that checks that a list of elementsl
of type'a
is sorted, according to an ordering functioncmp
of type'a -> 'a -> int
. The ordering function returns:
1
(or any positive number) if the first element is greater than the second,-1
(or any negative number) if the first element is lesser than the second,- and
0
otherwise.
For the fold_left
part, you can use the type 'a option
as the accumulator: at each iteration of fold_left
, if the list if sorted until now, the acccumulator is either Some v
, where v
is the previous element, or None
otherwise.
Remember, the empty list is sorted, so you can use the list with at least one element to check using fold_left
.
let for_all p l =
"Replace this string with your implementation."
let exists p l =
"Replace this string with your implementation."
let sorted cmp l =
"Replace this stirng with your implementation."