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 can be easily written with elem
, I have a strong feeling that I can use the syntax Arrow
to simplify the lambda, I just cannot get it right.
Can this lambda be simplified using arrows? And do you have general clues on how to "see" when arrows might work, and how to find the correct expression?
+3
source to share
2 answers
Pull the calculation out of foldr -
ghci> :m +Control.Arrow
ghci> any (==2) &&& any (==7) $ [1..6]
(True,False)
But if you want to be sure that you only scan 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)
+7
source to share