[Haskell-beginners] some insights into functional programming
Michael Mossey
mpm at alumni.caltech.edu
Sun Aug 9 15:31:47 EDT 2009
I'm starting to figure out a few things that I didn't "get" about
functional programming and monads. I wanted to explain them. I'm not
looking for a particular response to this post, other than any elaboration
that seems natural.
There is an exercise here working with the trivial monad W:
<http://blog.sigfpe.com/2007/04/trivial-monad.html>
Write a function
g :: W a -> W a -> W a
such that
g (W x) (W y) = W (x+y)
except don't use pattern matching, but >>= instead. The answer is
g mx my = mx >>= (\x -> my >>= \y -> W (x+y))
There are a couple things here that threw me off. One is that I didn't
expect 'my' to be available inside the first lambda. I somehow thought of
lambda as isolated, sealed-off from the rest of the universe. But they
aren't. I believe this is the concept of closures, or related to it?
Secondly, I didn't expect >>= to be available inside the lambda. This is
related to the mistaken conception of >>= as a procedural statement rather
than an expression. In Python, where I have previously encountered lambdas,
no statements are allowed inside lambdas. Of course, >>= is actually an
expression and you can put any expression to the right of a lambda ->.
Maybe these are typical beginner misconceptions, or maybe they have more to
do with coming from Python and complete beginners actually find it more
natural.
Mike
More information about the Beginners
mailing list