[Haskell-cafe] Re: wanted: haskell one-liners (in the perl sense of one-liners)

Lutz Donnerhacke lutz at iks-jena.de
Tue Mar 20 08:50:01 EDT 2007

* Thomas Hartman wrote:
> You contributed
> #12:14 < Thunder> @pl s -> drop (length s - n) s
> #12:14 < lambdabot> drop =<< subtract n . length
> But, on second thought, 1) I can't use this as a drop-in replacement
> for the non points free (right term?) version, and 2) I don't really
> understand it.

I did not contribute, but polluted the channel with my own test.

In order to understand this construct you have to switch to the (-> a) Monad:

     drop =<< subtract n . length

==   do x <- subtract n . length
     	drop x

This construct consists of partially applied functions, i.e. functions
waiting for an argument.

If you apply an argument to the whole construct, it is applied to each line

So  "drop =<< subtract n . length $ s" becomes:

     let x = subtract n . length $ s
     in  drop x s


     let x = length s - n
     in  drop x s

     drop (length s - n) s

I did not expect this monadic approach from lambdabot and was somewhat
surprised. I assumed an application of "liftM2 drop" instead.

More information about the Haskell-Cafe mailing list