is an option. When a closure was needed, it was customary to load the
CL package and use lexical-let.
It would be very nice if lexical-let were added to newLISP. Closures
could be created more easily, by a method that is more readily
understood by users of other dialects of Lisp. It would make it easier
to demonstrate the power of newLISP to potential converts.
There is a book titled "Let over Lambda" that demonstrates the
advanced use of closures; when lexical-let is added to newLISP,
converting the code in the book will be a trivial task.
The amount of code added to the newLISP distribution would be
miniscule. It's much better to have the facility bundled with the
language rather than having to include the code for the macro when
you post an example in a forum like Stackoverflow. Brevity will make a
better impression on interested programmers.
I know that closures can be created using contexts, but it seems to me
that most programmers will find it easier to use a lexical let; they
are used to doing it that way in other Lisps and in Scheme. Since it
makes programming easier and adding it to the language would be dead
simple, it would be a shame not to include it.
To get the ball rolling, here is my attempt at an implementation.
(uuid is used instead of gensym.)
Code: Select all
(context 'lexical-let) (define-macro (lexical-let:lexical-let varval-pairs) (let (body (cons 'begin (args)) alist (map (fn(x) (list (x 0) (sym (string 's (uuid))) (eval (x 1)))) (explode varval-pairs 2))) (bind (map rest alist)) (dolist (x alist) (set-ref-all (x 0) body (x 1))) (eval body))) (context MAIN) ;; Example (define (make-fibber) (lexical-let (a 0 b 1) (fn() (swap a b) (++ b a) a)))
increased, by adding an implementation of this.