Question: Define the following polymorphic F# functions on lists using the foldr function for lists: filter : (a -> bool) -> (a list -> a list)
Define the following polymorphic F# functions on lists using the foldr function for lists:
filter : (a -> bool) -> (a list -> a list) where filter p xs applies p to all elements x of xs and returns a list of those for which p x is true.
forall : (a -> bool) -> (a list -> bool) where forall p xs applies p to each element x of xs and returns true if all the results are true.
exists : (a -> bool) -> (a list -> bool) where exists p xs applies p to each element x of xs and returns true if any of the results is true.
mapPartial: (a -> b option) -> (a list -> b list) where mapPartial f xs applies f to all elements x of xs and returns a list of the values y for which f x has form Some y. You can think of mapPartial as a mixture of map and filter, where None corresponds to false and Some y corresponds to true. Thus
mapPartial (fun i -> if i>7 then Some(i-7) else None) [4; 12; 3; 17; 10]
should give [5; 10; 3].
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
