[Edu-sig] pyscheme
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Wed, 2 Jan 2002 01:43:42 -0800 (PST)
> > I've been tinkering with Scheme and Python for a while now, and I've
> > cooked up a translation of the interpreter from the textbook "The
> > Structure and Interpretation of Computer Programs" into Python:
>
>
> That's impressive.
>
> I fed it the quine
>
> ((lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list
> x (list (quote quote) x)))))
>
> and was impressed that I got back "the same" expression.
> Unfortunately, the notation is different, using Python lists and
> Python quotation instead of Scheme lists and Scheme quotation. The
> result, since the interpreter won't accept Python lists as input, is
> that you can't _actually_ write a program in a simple way which prints
> itself out character-for-character.
Ah! Ok, here's a function that translates Python structures into
scheme-looking strings:
###
def expressionToString(expr):
if not isList(expr):
return str(expr)
if isCompoundProcedure(expr):
return expressionToString([Symbol('compound-procedure'),
procedureParameters(expr),
procedureBody(expr)])
return '(' + string.join(map(expressionToString, expr), ' ') + ')'
###
This function also summarises compound procedures so thtat only their
parameters and body will show, and does the work that USER-PRINT did in
the original metacircular evaluator.
I'll sandwich expressionToString() between output to the user, and that
should allow quine-expressions to work properly. With the modifications,
the self-quining code now does this:
###
[PyScheme] >>> ((lambda (x) (list x (list (quote quote) x))) (quote
(lambda (x) (list x (list (quote quote) x)))))
((LAMBDA (X) (LIST X (LIST (QUOTE QUOTE) X))) (QUOTE (LAMBDA (X) (LIST X
(LIST (QUOTE QUOTE) X))))
###
The updated code is at:
http://hkn.eecs.berkeley.edu/~dyoo/python/pyscheme/pyscheme-0.6.tar.gz
Thanks for the suggestion!