Search code examples
haskellarrow-abstraction

How do I use arrows here?


Consider

foldr (\x (a,b) -> (a || x==2, b || x==7 )) (False,False) [1..6]
--(True,False)

Ignoring the fact that this could be written easily using elem, I have the strong feeling that I could employ Arrow syntax to simplify the lambda, I just can't get it right.

Can this lambda be simplified using arrows? And do you have any general hints concerning how to "see" when arrows might work, and how to find the right expression?


Solution

  • Pull the computation out of the foldr -

    ghci> :m +Control.Arrow
    ghci> any (==2) &&& any (==7) $ [1..6]
    (True,False)
    

    But if you want to be sure you're only traversing the list once, try using the bifunctor package:

    ghci> :m +Data.Bifunctor +Data.Bifunctor.Apply
    ghci> foldr (bilift2 (||) (||) . ((==2) &&& (==7))) (False, False) [1..6]
    (True,False)