I am trying to solve this question.
type alias FunSet = Int -> Bool
contains : FunSet -> Int -> Bool
contains set elem = set elem
singletonSet : Int -> FunSet
singletonSet elem = \inputElem -> elem == inputElem
union : FunSet -> FunSet -> FunSet
union a b = (\x -> (contains a x) || (contains b x))
My fold function should do the following:
Takes a list of sets and returns a new set, which is build by applying a fold using operation
function.
fold: List FunSet -> ( FunSet -> FunSet -> FunSet ) -> FunSet
fold operation sets =
Example:
(fold union [(singletonSet 1), (singletonSet 2), (singletonSet 3)]) 1 == True
It is not clear to me how should I do this. Hope someone can help ^^
You can use List.foldl
or List.foldr
for this:
fold sets f =
case sets of
[] -> \_ -> False
x :: xs -> List.foldl f x xs
I feel it would be easier to define emptySet _ = False
and directly use List.foldl
:
List.foldl union emptySet [(singletonSet 1), (singletonSet 2), (singletonSet 3)]
Note that the first parameter of List.foldl
is a function that takes first an element and then the accumulator, rather than the other way around, so you can't simply use fold sets diff
, you'd have to use fold sets (flip diff)
to flip the arguments.