merits of Lisp vs Python

Alex Mizrahi udodenko at users.sourceforge.net
Sat Dec 9 09:24:58 EST 2006


(message (Hello 'Paul)
(you :wrote  :on '(09 Dec 2006 02:55:49 -0800))
(

 PR> "Alex Mizrahi" <udodenko at users.sourceforge.net> writes:
 ??>> we can implement Scheme's call-with-current-continuation first :)
 ??>> it's relatively easy -- just a code walker that coverts everyting into
 ??>> CPS.

 PR> It's not enough to convert to CPS, you have to be able to actually
 PR> save the continuation when you switch to another one, so you can go
 PR> back to the first one later.  Maybe I'm missing something but I just
 PR> don't see how to do that in the Lisp execution model.

once you convert code to CPS, you can save continuation -- that would be a 
simple closure.
let's check how it works using CPS from ARNESI library.

(setq *cc* (with-call/cc (print 1) (let/cc cc cc) (print 3)))

that prints 1 and returns continuation.
when we call that contination:

(funcall *cc* nil)

it prints 3.

we can do a generator now:

(defun gen1 ()
  (let (state)
    (setf state
   (lambda (ignored)
     (with-call/cc
      (loop for i upfrom 1
        do (let/cc cc
     (setf state cc)
     i)))))
    (lambda () (funcall state nil))))


(setq *gen* (gen1))

(funcall *gen*) => 1
(funcall *gen*) => 2
(funcall *gen*) => 3

then we can make a defgenerator macro so it will look like:

(defgenerator gen1 ()
    (loop for i upfrom 1 do (yield i))

certainly yield is (let/cc cc (setf state cc) value)

 PR>   I guess you could write an interpreter in Lisp that simulates it all,
 PR> but it might as well be a Python interpreter ;-).

no, we can just transform code, and it can be then compiled or whatever.
there is only additional overhead (lambda and friends) for each call/cc. all 
"continuous" forms can be efficiently compiled.

)
(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