[Haskell-beginners] some insights into functional programming

Adam Bergmark adam at edea.se
Sun Aug 9 21:07:44 EDT 2009


The reason my is available in the lambda \x -> my >>= \y -> W (x+y)  
has to do with scoping rules, Haskell (and almost all programming  
languages) use static scoping, meaning a variable defined in an outer  
function is available in the inner function, for instance, (\a -> \b - 
 > (a,b)) 1 2 will evauate to (1,2) since a is bound to 1 in the outer  
lambda, and the inner one can refer to the outer one, if instead you  
write (\a -> \a -> (a,a)) 1 2 the result will be (2,2) since the  
innermost a will be used (no ambiguity here, but  if shadowing is done  
by accident it can be hard to find the error). The book Structure and  
Interpretation of Computer Programs (freely available online)  
discusses this subject in detail.

 >>= is available inside the lambda for the same reason, >>= is  
imported into the modules namespace, and therefore available  
everewhere, unless a shadowing binding exists.

I wouldn't say that this has anything to do with functional  
programming specifically.

This also exists in Python i might add, you can read variables defined  
in an outer scope:
 >>> a = 1
 >>> f()
 >>> def f():
...   return a
...
 >>> f()
1

The fact that most languages are scoped like this is nothing obvious,  
and like I said, Python is the same.


I hope this helps!

/ Adam


On Aug 9, 2009, at 9:31 PM, Michael Mossey wrote:

> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



More information about the Beginners mailing list