[Haskell-cafe] Assignment, Substitution or what?

Jules Bean jules at jellybean.co.uk
Wed Oct 3 07:09:08 EDT 2007

PR Stanley wrote:
> 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?

It is not, in my opinion, an unreasonable intuition.

What's interesting to note is that because haskell values are immutable, 
there is no need for this to be a *new* memory location. In fact, in a 
typical simple haskell implementation what f is actually passed is a 
pointer to to the existing memory location. Procedure call in haskell 
doesn't normally involve argument copying, because with immutability 
there is no need for copying.

Of course 'x*x' is a new value, so new memory is definitely allocated 
for that.

The next epiphany of understanding is when you realise that what 
actually goes into that new memory is a code pointer, rather than a 
value. Instead of calculating 'x*x' the compiler simply chucks a pointer 
to the code which, when called, will calculate x*x. This code is only 
called if needed.


More information about the Haskell-Cafe mailing list