
Hi f x = x + x Is the "x" use to create a pattern in the definition and when f is called it's replaced by a value? What's actually happening here? Thanks, Paul

PR Stanley wrote:
Hi f x = x + x Is the "x" use to create a pattern in the definition and when f is called it's replaced by a value?
Basically, uh, yeah. If you say "f 5", this is basically equivilent to "5 + 5" by the above definition. (I'm sure a huge number of others will chime in on this one too...)

If you've never been exposed to lambda calculus, then you're in for a real treat! There is no shortage of tutorials on this. If Greek letters and symbol manipulations are not your thing, take a look at http://worrydream.com/AlligatorEggs/ which describes a game that teaches children about lambda calculus. It is fun to read even for experts. I got the above link off the Wikipedia page http://en.wikipedia.org/wiki/Lambda_abstraction which you can take a look at when you're done learning how alligators each other, lay eggs, and die, in color! PR Stanley wrote:
f x = x + x Is the "x" use to create a pattern in the definition and when f is called it's replaced by a value?
Those equation-like definitions are syntactic sugar for lambda abstractions. f could as well be defined as f = \x -> x + x.
Please elaborate _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 10/1/07, PR Stanley
f x = x + x Is the "x" use to create a pattern in the definition and when f is called it's replaced by a value?
Those equation-like definitions are syntactic sugar for lambda abstractions. f could as well be defined as f = \x -> x + x.
Please elaborate
First, the f x = part says that f is a function which takes a single parameter, called x. The other side of the = sign gives the function body: in this case, x + x. This is exactly the same thing that is expressed by the lambda expression \x -> x + x This expression defines a function that takes a single parameter called x, and returns the value of x + x. The only difference is that with the lambda expression, this function is not given a name. But you can easily give the function a name (just as you can give any Haskell expression a name) by writing f = \x -> x + x In general, writing g x y z = blah blah is just a shorthand for g = \x -> \y -> \z -> blah blah. That is, it simultaneously creates a function expression, and assigns it a name. Does that help? -Brent
participants (5)
-
Andrew Coppin
-
Brent Yorgey
-
Dan Weston
-
Malte Milatz
-
PR Stanley