How can I elegantly not do this. anyone in Haskell?

I am trying to figure out how to nullify the results of two boolean functions for example not . any

. I understand why it didn't work by breaking it down like below, but I'm not sure how to write a function that does this elegantly. I managed to docurry $ not . uncurry any

Prelude> :t not
not :: Bool -> Bool

Prelude> :t any
any :: Foldable t => (a -> Bool) -> t a -> Bool

Prelude> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c

curry $ not . uncurry any
:: Foldable t => (a -> Bool) -> t a -> Bool

      

+3


source to share


3 answers


There is a standard point-free-ifier available separately or via lambdabot which gives:

18:02 <dmwit> ?pl \f xs -> any (\x -> not (f x)) xs
18:02 <lambdabot> any . (not .)
18:04 <dmwit> ?pl \f xs -> not (any f xs)
18:04 <lambdabot> (not .) . any

      



There are many ways to write this general operation .

Edit: Thanks to zudov for the following suggested additional text: you can also access the tool pointfree

by installing pointfree

or using one of the web interfaces (e.g. http://pointfree.io ).

+11


source


Define

result = (.)
argument = flip (.)

      

Then you want

(result.result) not any

      

ie, negating the second result any

.



(It's the same

argument (result not) all

      

ie, negate the predicate, then pass it to all

).

See http://conal.net/blog/posts/semantic-editor-combinators

+6


source


(.:) :: (r -> z) -> (a -> b -> r) -> a -> b -> z
(f .: g) x y = f (g x y)

foo :: (a -> Bool) -> [a] -> Bool
foo = not .: any

      

.:

also available Data.Composition

from the package composition

.

+5


source







All Articles