[Haskell-beginners] Value access in lambda function

Emanuel Koczwara poczta at emanuelkoczwara.pl
Sat Dec 29 15:26:43 CET 2012


Hi,


> Brandon, I need to study this again, still dont understood how its
> related to partial function application.
> 


Take a look at:
http://learnyouahaskell.com/higher-order-functions#lambdas

addThree :: (Num a) => a -> a -> a -> a  
addThree x y z = x + y + z  

can be defined as:

addThree :: (Num a) => a -> a -> a -> a  
addThree = \x -> \y -> \z -> x + y + z

and with brackets it would be:

addThree = \x -> (\y -> (\z -> (x + y + z)))

Now, you just need to put brackets to see what's going on there (i've
simplified your code for clarity):

t v = Just v >>? \v1 -> Just (v + 1) >>? \v2 -> Just (v + v1 + v2)

This would be (it actually is!):

t v = Just v >>? \v1 -> (Just (v + 1) >>? \v2 -> (Just (v + v1 + v2) ) )

Now, if you will put braces in wrong place:

t v = Just v >>? (\v1 -> Just (v + 1)) >>? (\v2 -> Just (v + v1 + v2))

Then you will get: Not in scope: `v1'


> Also consider this:  in Haskell, a function is a binding like any
> other.  If you couldn't refer to bindings from outer scopes, you
> couldn't define new functions!  Well, aside from anonymous
> ones/lambdas, which are useful but somewhat limiting if you can't name
> and reuse them.
> 
> 
> Brandon, This is quite strange thing, the functions can access the
> globally defined variables and therefore there output is dependent not
> just on inputs but on those variables too. This goes against the
> understanding of pure functions. 


These 'global expressions' do not change during execution, output is
dependent only on input, and function could be defined in terms of
arguments and some 'constants' ('global expressions') and other
functions (also 'global expressions' (there is no difference).

Emanuel






More information about the Beginners mailing list