[Tutor] Learning Python using other languages --- round two
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Wed, 23 Jan 2002 23:34:15 -0800 (PST)
[For people who are beginning to learn Python; skip this message. This is
about something else. *grin*]
Hi everyone,
A while back, I wrote about thinking about writing a Scheme interpreter in
Python.
http://aspn.activestate.com/ASPN/Mail/Message/811896
Well, I just couldn't hold back from unleashing this monstrosity. I bring
to you... Pyscheme!
http://hkn.eecs.berkeley.edu/~dyoo/python/pyscheme/
I thought it might be interesting for anyone who'd like to see an example
of writing one language in terms of another. Here's an example of it in
action:
###
dyoo@einfall:~/pyscheme-1.0/pyscheme$ python scheme.py
Welcome to PyScheme! Type: (QUIT) to quit.
[PyScheme] >>> (define (square x) (* x x))
ok
[PyScheme] >>> (define (cube x) (* (square x) x))
ok
[PyScheme] >>> square
(compound-procedure (x) ((* x x)) <procedure-env>)
[PyScheme] >>> cube
(compound-procedure (x) ((* (square x) x)) <procedure-env>)
[PyScheme] >>> (cube 2)
8
[PyScheme] >>> (cube 2000)
8000000000
[PyScheme] >>> (define (factorial x)
[......1)] >>> (if (= x 0)
[......2)] >>> 1
[......2)] >>> (* x (factorial (- x 1)))))
ok
[PyScheme] >>> (factorial 2)
2
[PyScheme] >>> (factorial 3)
6
[PyScheme] >>> (factorial 4)
24
[PyScheme] >>> (quit)
BYE
###
So if you ever wanted to look into the guts of a really ugly program,
Pyscheme is now a candidate. *grin* I'll have to study Python a little
more, but I'd be really happy if I can get tail recursion to work
properly... Anyway, this was my side project for the winter, so I wanted
to share it with the people here.