[Haskell-cafe] Does this function have a name?

John Wiegley johnw at newartisans.com
Sat Apr 8 21:20:49 UTC 2017

>>>>> "PB" == Paul Brauner <polux2001 at gmail.com> writes:

PB> Oops, there's a free variable in there, let me fix it:
PB> first f [] = Nothing
PB> first f (x:xs) = fmap (:xs) (f x) <|> fmap (x:) (first f xs)

If you break up the test from the modification (instead of fusing the
operation into 'f'), this sort of thing becomes quite easy to express
using lens:

  [1..10] & partsOf (traverse.filtered (>2))._head +~ 3

If the list has no elements >2, the operation is a no-op.

John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2

More information about the Haskell-Cafe mailing list