Re: How to implement lexical closures?

George Neuner <gneuner2@comcast.net>
Sat, 15 May 2010 18:24:36 -0400

          From comp.compilers

Related articles
[4 earlier articles]
Re: How to implement lexical closures? torbenm@diku.dk (2010-05-10)
Re: How to implement lexical closures? grom358@gmail.com (grom) (2010-05-11)
Re: How to implement lexical closures? cr88192@hotmail.com (BGB / cr88192) (2010-05-12)
Re: How to implement lexical closures? gene.ressler@gmail.com (Gene) (2010-05-12)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-13)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-15)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-15)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-16)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-17)
Re: How to implement lexical closures? cfc@shell01.TheWorld.com (Chris F Clark) (2010-05-17)
Re: PL/I, was How to implement lexical closures? genew@ocis.net (Gene Wirchenko) (2010-05-17)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-19)
Re: How to implement lexical closures? anton@mips.complang.tuwien.ac.at (2010-05-20)
| List of all articles for this month |
From: George Neuner <gneuner2@comcast.net>
Newsgroups: comp.compilers
Date: Sat, 15 May 2010 18:24:36 -0400
Organization: A noiseless patient Spider
References: 10-05-031 10-05-072 10-05-076 10-05-084
Keywords: storage, symbols
Posted-Date: 16 May 2010 01:52:26 EDT

On Sat, 15 May 2010 03:52:50 -0400, George Neuner
<gneuner2@comcast.net> wrote:


Our esteemed moderator wrote:


>[PL/I doesn't do garbage collection, so you can't use an entry
>outside the block where its variables were allocated. Looks to
>me like this came straight out of Algol60. -John]


Strictly speaking, GC isn't necessary ... if you are masochistic
enough, closures certainly can be managed manually like any other
program object.


In languages like standard Scheme and ML, which have closures but are
not OO, it is traditional to roll your own objects using closures.
Anonymous closures that represent the "methods" of the "object" are
typically packaged into an array or structure and are accompanied by
named helper functions as in:


    (define (make-queue)
        (let ((head '())
                    (tail '()))
            (vector
                (lambda () head); show function
                (lambda (obj) ; put function
                    (cond
                        ((pair? tail)
                          (set-cdr! tail (cons obj '()))
                          (set! tail (cdr tail)))
                        (#t
                          (set! head (cons obj '()))
                          (set! tail head))
                        ))
                (lambda () ; get function
                    (if (pair? head)
                            (let ((obj (car head)))
                                (set! head (cdr head))
                                  obj)
                            'EMPTY))
                (lambda () ; peek function
                    (if (pair? head)
                            (car head)
                            'EMPTY))
          )))


    (define (queue-show q) ((vector-ref q 0)))
    (define (queue-put q obj) ((vector-ref q 1) obj))
    (define (queue-get q) ((vector-ref q 2)))
    (define (queue-peek q) ((vector-ref q 3)))




A queue object then is an vector of 4 closures which share 'head' and
'tail' variables. Although they share state, the use of closures in
this manner is reasonably self contained and the resulting "object"
can be deleted with the same caveats that exist for deleting any data.


George


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.