Re: What's wrong with Common Lisp's lambda ?



In article <XSRSg.101055$iq2.1403931@xxxxxxxxxxxxxxxxxxxxx>,
"Sacha" <no@xxxxxxxxxxxx> wrote:

Common Lisp adopted some features of Scheme's lambda, so it doesn't have
as many problems as earlier dialects of Lisp. But there are still
holes. Although CL has lexical extent for variable bindings, it only
has dynamic extent for control bindings. So you can't do:

(defun make-bad-closure ()
(tagbody
label
(print 'got-here)
(lambda () (go label))))

(funcall (make-bad-closure))

Your code has a bug in it; tagbody returns nil, not the lambda.
If we fix that, and execute it piecemeal, then it becomes clear why
CL didn't take this particular aspect of Scheme:

CL-USER(1): (defun make-bad-closure ()
(tagbody
label
(print 'got-here)
(return-from make-bad-closure (lambda () (go label)))))
MAKE-BAD-CLOSURE
CL-USER(2): (make-bad-closure)

GOT-HERE
#<Interpreted Closure (:INTERNAL MAKE-BAD-CLOSURE) @ #x4071fe7a>
CL-USER(3): (funcall *)
Error: Cannot go to LABEL, its body has been exited.
[condition type: CONTROL-ERROR]

Restart actions (select using :continue):
0: Return to Top Level (an "abort" restart).
1: Abort entirely from this (lisp) process.
[1] CL-USER(4):


This raises the question : how would you do such thing using common lisp ?

Instead of returning a closure from inside the tagbody, you return one
that CONTAINS the tagbody, enters it, and jumps to the label. But I'm
having a hard time coming up with a good example.

More likely, though, you use an algorithm that doesn't require this in
the first place. Similar to what you do when you use languages that
don't have lexical closures.

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Re: Whats wrong with Common Lisps lambda ?
    ... as many problems as earlier dialects of Lisp. ... Although CL has lexical extent for variable bindings, ...
    (comp.lang.lisp)
  • Re: Jon Harrop rewrite benchmark; Qi, Lisp and OCaml
    ... Dr Harrop also provided the Lisp source ... Language: OCaml ... (g (simplify-no-redundant-checks y)) ... (TAGBODY ...
    (comp.lang.functional)
  • Re: lisp equivalent to c++ continue
    ... the same as a goto to a hidden label: ... In Lisp terms, ... The symbol NIL can be used to name blocks. ... > Should I be coding in a different way that doesn't require this keyword? ...
    (comp.lang.lisp)
  • Re: Lisp an abstraction layer?
    ... > field of business application. ... We should label our products with a big: ... and speak of lisp only within the knowledgable circle... ... -- Robert Heinlein ...
    (comp.lang.lisp)