TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to Write a Lisp Interpreter in Python (2010)

144 pointsby yankoffalmost 11 years ago

16 comments

ColinWrightalmost 11 years ago
There are lengthy discussions every time this is submitted[0] to HN - it&#x27;s one of the items that makes me think we need some sort of &quot;Hall of Fame&quot; or similar.<p>There&#x27;s also the follow-up[1]:<p><pre><code> (How to Write a ((Better) Lisp) Interpreter (in Python)) (norvig.com) </code></pre> Unsurprisingly, that also gets a good discussion when submitted[2], but since that discussion is closed, and this discussion has started again, I thought I&#x27;d re-submit that one[3] (partly as an experiment).<p>--------<p>[0] <a href="https://hn.algolia.com/?q=lisp+python#!/story/sort_by_date/prefix/0/write%20lisp%20interpreter%20python" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?q=lisp+python#!&#x2F;story&#x2F;sort_by_date&#x2F;p...</a><p>[1] <a href="http://norvig.com/lispy2.html" rel="nofollow">http:&#x2F;&#x2F;norvig.com&#x2F;lispy2.html</a><p>[2] <a href="https://news.ycombinator.com/item?id=1746916" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=1746916</a><p>[3] <a href="https://news.ycombinator.com/item?id=7825526" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7825526</a>
wfnalmost 11 years ago
I had once started writing a Lisp interpreter in Python, only to realize that I was leveraging a lot of pythonic power (most notably at least for me at the time - garbage collection) - i.e. more than I had planned to use.<p>So then I switched to writing the same interpreter in C, and building my own memory manager. (As is usually the case --) turns out it&#x27;s more complicated (for someone with little experience in writing these sorts of things) than one might expect. :) But it&#x27;s very rewarding indeed.<p>Of course I still think that there&#x27;s much value in writing such an interpreter in Python. I would simply recommend for someone doing that to later on re-write it in some lower level language!<p>Re: this, Norvig commented&#x2F;said,<p>&gt; <i>[...] we are relying on many features of Python: call stack, data types, garbage collection, etc. The next step would be to show a compiler to some sort of assembly language. I think either the Java JVM or the Python byte code would be good targets. We&#x27;d also need a runtime system with GC. I show the compiler in my PAIP book.</i>
评论 #7826120 未加载
jgrodziskialmost 11 years ago
A good complement to Norvig&#x27;s writings (actually inspired by it) is &quot;Lisp as the Maxwell&#x27;s equations of software&quot; by Michael Nielsen: <a href="http://www.michaelnielsen.org/ddi/lisp-as-the-maxwells-equations-of-software/" rel="nofollow">http:&#x2F;&#x2F;www.michaelnielsen.org&#x2F;ddi&#x2F;lisp-as-the-maxwells-equat...</a> Worth reading!
cessoralmost 11 years ago
There is a nice book by Terence Parr on &quot;The Pragmatic Bookshelf&quot; called &quot;Language Implementation Patterns&quot;. I found this to be a perfect introduction to compilers with a very practical point of view. Rather than talking about grammars for ages he dives right into some (java) code and shows you what problems come up when parsing code and how compilers solve them in different cases. I followed along in python and managed to write my own c-like compiler with it. I believe it is a perfect introduction for students that shows them that writing compilers is not dry, theoretical and boring, but interesting and fundamentally effectful. Terrence is by the way the author of Antlr (reference just for his street cred)
评论 #7825983 未加载
chrisloyalmost 11 years ago
Read this before, but always good to be reminded of a classic. I&#x27;d consider most of the essays on his front page essential reading, but I particularly enjoy the wit in his essay on writing a spelling corrector in Python [0], and the flattery of our collective egos in &#x27;Teach Yourself Programming in Ten Years&#x27; [1].<p>--------<p>[0] <a href="http://norvig.com/spell-correct.html" rel="nofollow">http:&#x2F;&#x2F;norvig.com&#x2F;spell-correct.html</a><p>[1] <a href="http://norvig.com/21-days.html" rel="nofollow">http:&#x2F;&#x2F;norvig.com&#x2F;21-days.html</a>
daGrevisalmost 11 years ago
I will try this out!<p>Also there is diy-lisp[0]. I myself did it and it was very rewarding and fun![1]<p>[0]: <a href="http://kjetilvalle.com/posts/implement-a-programming-language.html" rel="nofollow">http:&#x2F;&#x2F;kjetilvalle.com&#x2F;posts&#x2F;implement-a-programming-languag...</a><p>[1]: <a href="https://github.com/daGrevis/diy-lisp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;daGrevis&#x2F;diy-lisp</a>
评论 #7825769 未加载
userbinatoralmost 11 years ago
If you want to go the other way, there&#x27;s also a Python implementation in Lisp; unfortunately, without a nice article on how it was written: <a href="http://common-lisp.net/project/clpython/" rel="nofollow">http:&#x2F;&#x2F;common-lisp.net&#x2F;project&#x2F;clpython&#x2F;</a>
评论 #7827151 未加载
dschiptsovalmost 11 years ago
mr. Norvig is a great man, no doubts. Have you read AIMA, by the way? I did.)<p>This, of course, is not a &quot;complete&quot; Lisp. Let&#x27;s say that [one of] the most fundamental feature of a Lisp is ability to define a new &quot;special form&quot; (without modifying the code of the interpreter) as a [reader] macro. This is <i>the</i> way of defining a DSL. the loop constructs, structures, even whole CLOS is nothing but a DSL.<p>All we need is quote, quasi-quote, unquote, unquote-splicing, the &quot;conses-aware&quot; read function which &quot;expands&quot; these and the if special form that &quot;short-circuits&quot;.<p><i>This</i> (everything is made out of conses + general evaluation rule + reader macros) could be called a Lisp.<p>Want to see a Lisp? Look at arc.arc
评论 #7826991 未加载
评论 #7825669 未加载
评论 #7826039 未加载
jgrodziskialmost 11 years ago
Another recommended reading is the book &quot;Understanding Computation&quot; that has a section dedicated to Lambda Calculus: <a href="http://computationbook.com/" rel="nofollow">http:&#x2F;&#x2F;computationbook.com&#x2F;</a>
orangeduckalmost 11 years ago
Anyone who likes this idea but wants to do it in C might enjoy this <a href="http://www.buildyourownlisp.com/" rel="nofollow">http:&#x2F;&#x2F;www.buildyourownlisp.com&#x2F;</a>
leephillipsalmost 11 years ago
Very interesting, as you might imagine.<p>Actually, LaTeX was introduced in 1984, when the author began his thesis, and TeX had been out for years. But they did not yet seem to be widely known. I began my thesis a few years after Norvig, and I started with troff, which was still the standard, but had terrible output. Then someone told me about TeX, and I wrote my whole thesis in plain TeX, using a Mac Plus (that I still have and that still works).
anaphoralmost 11 years ago
Exercise for the reader: how might the representation Norvig uses for environments be changed if we assume all data is immutable? :)
basytalmost 11 years ago
Nobody&#x27;s heard of Hy then?<p><a href="https://github.com/hylang/tryhy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hylang&#x2F;tryhy</a>
评论 #7826739 未加载
评论 #7826494 未加载
scribualmost 11 years ago
And here I was thinking that I&#x27;d been the first to have the kooky idea of implementing Scheme in Python: <a href="https://github.com/scribu/scheme.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;scribu&#x2F;scheme.py</a> :)<p>It was a really rewarding experience; definitely recommended!
Monkeygetalmost 11 years ago
I love Norvig&#x27;s writing. He has a way of writing in concisely and make code appear effortless.
评论 #7826571 未加载
alixanderalmost 11 years ago
<a href="http://www-inst.eecs.berkeley.edu/~cs61a/sp14/proj/scheme/scheme.html" rel="nofollow">http:&#x2F;&#x2F;www-inst.eecs.berkeley.edu&#x2F;~cs61a&#x2F;sp14&#x2F;proj&#x2F;scheme&#x2F;sc...</a>
评论 #7825449 未加载
评论 #7825481 未加载