merits of Lisp vs Python
Alex Mizrahi
udodenko at users.sourceforge.net
Mon Dec 11 06:11:19 EST 2006
(message (Hello 'Paul)
(you :wrote :on '(10 Dec 2006 21:06:56 -0800))
(
??>> read the book.
PR> Which book?
Paul Graham's "On Lisp".
or just refreshing your knowledge about CPS transformaction might be
sufficient.
i've found very good explanation of CPS transformaction in the book
"Programming Languages:Application and Interpretation"
Shriram Krishnamurthi
Brown University
it's not Common Lisp but Scheme though, but it's very interesting since it
shows how continuations can be used for better web programming.
both books are available online.
??>> but once you convert it to CPS, you just operate with closures. stack
??>> is just a lexical variables caught into closure. do you know what does
??>> CPS mean at all??
PR> I once understood the basic notion but confess to have never been
PR> clear on the fine points. However, I don't see how you can do it with
PR> CL closures since CL semantics do not guarantee tail recursion
PR> optimization. If you code a loop with closures and CPS, you will blow
PR> the stack.
most CL implementation do tail call optimization, unless you're running
debug mode -- in that case you'd prefer full stack.
however, it's possible to implement tail call optimizations in non-tail-call
optimizing language. i think it's called trampolined style.
example can be found in PAIP book: interpreter of Scheme is implemented in
Common Lisp.
CPS transformation found in ARNESI implements that -- you might notice on
the macroexpansion i've showed in prev example function drive-cps. it's
defined following way:
(defun drive-cps (cps-lambda)
(catch 'done
(loop for f = cps-lambda then (funcall f))))
additional (lambda () ...) is inserted in the code to delay evaluation. code
becomes like this:
(drive-cps
(block1)
(lambda () (block2))
thus for each CPS tear-point it's able to throw stale stack frames away.
here's an example of CPS-transformed eternal loop
(macroexpand '(with-call/cc (loop for i upfrom 1 do (print i) do (call/cc
(lambda (cont) cont)))))
(DRIVE-CPS
(LET ((#:CPS-LET-I-1712 1))
(DECLARE (TYPE NUMBER #:CPS-LET-I-1712))
(LABELS ((#:TAGBODY-TAG-NEXT-LOOP-1713 ()
(PROGN
(PRINT #:CPS-LET-I-1712)
(LAMBDA ()
(FUNCALL #'TOPLEVEL-K
(FUNCALL #'(LAMBDA (CONT) CONT)
(MAKE-CALL/CC-K
(LAMBDA (#:V-1717)
(DECLARE (IGNORE #:V-1717))
(PROGN
(PROGN
(SETQ #:CPS-LET-I-1712
(1+ #:CPS-LET-I-1712)))
(#:TAGBODY-TAG-NEXT-LOOP-1713))))))))))
(#:TAGBODY-TAG-NEXT-LOOP-1713))))
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"People who lust for the Feel of keys on their fingertips (c) Inity")
More information about the Python-list
mailing list