lexical lambda's where ever you want them

Notices and updates
Locked
jsmall
Posts: 26
Joined: Mon Sep 20, 2004 1:44 am

lexical lambda's where ever you want them

Post by jsmall »

NewLisp has a convenient way of synthesizing
lexical scope behavior whenever you need it.

Code: Select all


  (define z 3)

  (define foo
    (let ((y 2))
      (expand (lambda (x) (list x y z)) 'y 'z)))

  (foo 1)  ;; ==> (1 2 3)

The "expand" built-in function takes its first
argument, e.g. the lambda expression here, and
expands all symbols within it to the values
calculated by evaluating the remaining
symbolic arguments.

Since the dynamic scope of "expand" is also the
lexical scope of lambda this results in the
expected behavior. Thus the "foo" above is bound
to the following lambda list:

Code: Select all


    > foo
    (lambda (x) (list x 2 3))

The following convenience macro expand-let lets
you set up lexical scope behavior wherever.

Code: Select all


  (define z 3)

  (define foo
    (expand-let
      (lambda (x) (list x y z))
      (y 2) (z z)))

Now you can whichever scoping strategy you want,
lexical or dynamic.

Code: Select all


  (define-macro (expand-let ) 
    (eval 
      (cons 'let 
        (cons (rest (args)) 
          (list (cons 'expand 
            (cons (first (args)) 
              (map (fn (arg) (apply quote (list (first arg)))) 
                   (rest (args))))))))))


Locked