What am I doing wrong with Set.Fold F#

后端 未结 2 1260
走了就别回头了
走了就别回头了 2021-01-22 01:31

Colouring problem :

Hello, I\'m trying to implement a bool function that returns true when a color can be extended to a country and false otherwise but I\'m having troub

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 02:06

    To check that no element in a collection satisfies a given condition: that's not a job for fold, but, with appropriate negation, for exists or forall. Either of

        let fornone f = Set.forall (f >> not)
        let doesnotexist f = Set.exists f >> not
    

    will do, as shown in the answer of FuleSnabel. However, it is certainly possible to build these functions from a fold, albeit nobody would do that except as an illustration of currying, function composition and pointfree style.

        let fornone f = Set.fold (fun s -> f >> not >> (&&) s) true
        let doesnotexist f = Set.fold (fun s -> f >> (||) s) false >> not
    

提交回复
热议问题