Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 297
Landei Avatar asked Apr 26 '26 10:04

Landei


1 Answers

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)
like image 135
rampion Avatar answered Apr 29 '26 01:04

rampion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!