FunSet-Elm折叠操作

FunSet-Elm折叠操作,elm,fold,Elm,Fold,我正在努力解决这个问题 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 -&g

我正在努力解决这个问题

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))   

          
我的折叠功能应执行以下操作: 获取集合列表并返回一个新集合,该集合通过使用
操作
函数应用折叠来构建

fold: List FunSet -> ( FunSet -> FunSet -> FunSet ) -> FunSet
fold operation sets = 
例如:

(fold union [(singletonSet 1), (singletonSet 2), (singletonSet 3)]) 1 == True

我不清楚该怎么做。希望有人能帮忙^ ^

您可以使用
List.foldl
List.foldr
来实现以下目的:

fold sets f = 
  case sets of
    [] -> \_ -> False
    x :: xs -> List.foldl f x xs
我觉得定义
emptySet=False
和直接使用
List会更容易。foldl

List.foldl union emptySet [(singletonSet 1), (singletonSet 2), (singletonSet 3)]

请注意,
List.foldl
的第一个参数是一个函数,它首先接受一个元素,然后接受累加器,而不是相反,因此不能简单地使用
fold set diff
,必须使用
fold set(flip diff)
来翻转参数。

很好。我为工会工作。但是现在我有另一个问题,我也在尝试做同样的事情来改变现状。例如:(fold diff[setOf[1,2,3],setOf[1],setOf[2]])3==True@Myii您遇到了什么问题?例如,它应该返回True,但返回False。我也不太明白用折叠表示差异的概念。我有一个单独的函数diff:diff:FunSet->FunSet->FunSet diff a b=(\x->contains a x&&(not(contains b x)))。我该如何在这个问题上应用fold?@Myii这是因为争论的顺序。请尝试
(\b a->diff a b)