[Prev][Next][Index][Thread]

Re: Closures



In article <3C0997E9.D8CF8AA@ipeg.com>, Doug <dough@ipeg.com> wrote:

> Suppose that we change this part of the example
> slightly to use a variable rather than the constant
> 100, is this correct?
> 
> begin
>   let my-amount := 100
>   let (my-balance, my-deposit, my-calc-interest)
>     = make-account(my-amount, 3);
>   my-deposit(50);
>   my-calc-interest();
>   values (my-balance(), my-amount);
> end;
> 
> => 154.5, 154.5

Not quite.  my-calc-interest binds a *new* value to the variable, it 
doesn't modify the original value.  It should be 154.5, 100.

Think of *every* variable being a pointer to some malloc()'d memory 
containing the value.  Whenever you use ":=" you make the variable point 
to a different bit of memory.

In fact, integers and characters and single-preecision floating point 
doesn't quite work like that (for efficiency) but the compiler makes 
sure that the results are the same as if it *did* work that way.


What Dylan compiler do you use?  With Fuctional-Developer you can just 
open the playground and type or paste this stuff in.  With MacMarlais 
you can also enter it interactively.  With Mindy or d2c you need to go 
through a compile cycle, which is a pain, but at least it's only around 
8 - 10 seconds on a fairly modern Mac or PC (700+ MHz G4 or K7 or P3).

-- Bruce



References: