P. 1
Haskel Tutorial

Haskel Tutorial

|Views: 132|Likes:
Publicado pordevilvsevil

More info:

Published by: devilvsevil on Oct 23, 2008
Direitos Autorais:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





The name “Lambda Calculus”, while perhaps daunting, describes a fairly simple sys-
tem for representing functions. The way we would write a squaring function in lambda
calculusis: λx.x∗x, whichmeansthatwetakeavalue, whichwewillcallx(that’swhat
“λx. means) and then multiply it by itself. The λ is called “lambda abstraction.” In
general, lambdas can only have one parameter. If we want to write a function that takes
twonumbers, doublesthefirstandaddsittothesecond, wewouldwrite: λxλy.2∗x+y.
When we apply a value to a lambda expression, we remove the outermost λ and replace
every occurrence of the lambda variable with the value. For instance, if we evaluate
(λx.x∗x)5, we remove the lambda and replace every occurrence of x with 5, yielding
(5∗5) which is 25.

In fact, Haskell is largely based on an extension of the lambda calculus, and these
two expressions can be written directly in Haskell (we simply replace the λ with a
backslash and the . with an arrow; also we don’t need to repeat the lambdas; and, of
course, in Haskell we have to give them names if we’re defining functions):

square = \x -> x*x
f = \x y -> 2*x + y

You can also evaluate lambda expressions in your interactive shell:

Prelude> (\x -> x*x) 5
Prelude> (\x y -> 2*x + y) 5 4

We can see in the second example that we need to give the lambda abstraction two
arguments, one corresponding to x and the other corresponding to y.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->