Lexical Closures in Dylan 
Author Message
 Lexical Closures in Dylan

Can I use Dylan to create lexical closures similar to Scheme?
Can someone give me an example?  For instance, how would I code
the following Scheme example in Dylan?

Quote:
> (define incr-counter #f)
>(let ((foo 0))

    (set! incr-counter (lambda () (set! foo (+ foo 1)) foo))
 )

Quote:
>(incr-counter) => 1
>(incr-counter) => 2

etc...

BTW, I know that Dylan has modules, to avoid having people write
code like this.  If you show me how to do it, I promise never to
write code like that, at least not before midnight.

Thanks,
DDL

-------------------------------------------------
David D. Lowry
Dike, Bronstein Roberts & Cushman LLP

Sir Bors: ...and that is how we know the earth to be banana shaped.

King Arthur: This new learning amazes me!  Explain again how ram's
             bladder can be used to detect earthquakes.

M.P. and the H.G.



Mon, 22 Mar 1999 03:00:00 GMT  
 Lexical Closures in Dylan

Quote:
> Can I use Dylan to create lexical closures similar to Scheme?

Yes, Dylan has closures.

Quote:
> Can someone give me an example?  For instance, how would I code
> the following Scheme example in Dylan?

> > (define incr-counter #f)

> >(let ((foo 0))
>     (set! incr-counter (lambda () (set! foo (+ foo 1)) foo))
>  )

> >(incr-counter) => 1

> >(incr-counter) => 2

In fact, you can do it almost exactly the same way in Dylan (if I
understand your example correctly):

   define variable incr-counter = #f;

   begin
      let foo = 0;
      incr-counter := method () foo := foo + 1 end method;
   end;

Quote:
> BTW, I know that Dylan has modules, to avoid having people write
> code like this.  If you show me how to do it, I promise never to
> write code like that, at least not before midnight.

I'm not sure that modules obviate the need for this idiom. Perhaps you
could make the counter a module variable that is not exported from the
module, but you'll only get one counter.

...........................................................................
Chris Page
Software Wrangler                  This message was created using {*filter*}dog

http://www.*-*-*.com/ ~page/
...........................................................................



Tue, 23 Mar 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. OO, lexical scope, closure, tail-recursion, continuation

2. Are Agents Lexical Closures?

3. Lexical scope and closures

4. Nested scopes and lexical closures

5. Yet Another Idea for lexical closures in Python

6. Compiling lexical closures in Common Lisp

7. Lambda-expression and lexical closure

8. CL and lexical closures

9. Compiling lexical closures

10. lexical closures and python

11. archives of info-dylan/comp.lang.dylan available

12. Dylan vs DyLan

 

 
Powered by phpBB® Forum Software