
On Wed, 2007-10-03 at 01:42 +0100, 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
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?
Yes and thanks for the reply. When a function is declared in C the argument variable has an address somewhere in the memory: int f ( int x ) { return x * x; }
any value passed to f() is assigned to x. x is the identifier for a real slot in the memory (the stack most likely) made available for f(). Is this also what happens in Haskell?
How would you tell?