News for hackers. Stuff that matters.
Re:Pissed off about functional programming by icandoitbetter on Saturday March 26, 2011 @11:02PM (#2374019)
Can you explain the imperative code? I understand that it has to do with lists being monads and all, but I don't get why

    (return ([1..10]*2)) :: [Integer]
doesn't work. What's the magic behind

    x <- [1..10]

?
Re:Pissed off about functional programming by tkahn6 on Saturday March 26, 2011 @11:09PM (#2374031)
That `do` expressions is equivalent to

    [1..10] >>= (\x -> return (x * 2))
and the definition of (>>=) for a List monad is

    instance Monad [] where
      return a = [a]
      xs >>= f = concat (map f xs)
`xs` becomes bound to [1..10]

`f` becomes bound to (\x -> return (x * 2))

we we map `f` over each element in the list and get mini-lists. `x` gets bound to each element in the list, x is multiplied by 2, and then returned into a list (1 becomes [2], 2 becomes [4], and so on). So we get:

    concat [[2],[4],[6]..[20]]
And then we concatenate the mini-lists together to get.

    [2,4..20]
As an aside, this is also equivalent

   [1..10] >>= return . (* 2)
Re:Pissed off about functional programming by icandoitbetter on Saturday March 26, 2011 @11:23PM (#2374050)
Thanks!