(How to Write a (Lisp) Interpreter (in Python))
norvig.com
norvig.com
Actually, in his case, more like 35.
Granted, some of that ugliness is of our own creation... but not all of it by any means.
At university we had a copy on implementation of functional programming languages following "The Architecture of Symbolic Computers" by Peter Kogge, which is very good, too. (http://www.amazon.com/exec/obidos/ASIN/0070355967)
"I asserted that you could define the "most powerful language in the world" in "a page of code." I had orignally made the boast because McCarthy's self-describing LISP interpreter was written in itself. It was about "a page", and as far as power goes, LISP was the whole nine-yards for functional languages." --- Alan Kay in http://gagne.homedns.org/~tgagne/contrib/EarlyHistoryST.html
I thought this was a particularly interesting nugget:
""" Just as an aside, to give you an interesting benchmark—on roughly the same system, roughly optimized the same way, a benchmark from 1979 at Xerox PARC runs only 50 times faster today. Moore’s law has given us somewhere between 40,000 and 60,000 times improvement in that time. So there’s approximately a factor of 1,000 in efficiency that has been lost by bad CPU architectures.
The myth that it doesn’t matter what your processor architecture is—that Moore’s law will take care of you—is totally false. """
def tokenize(s):
"Convert a string into a list of tokens."
return s.replace('(',' ( ').replace(')',' ) ').split() def tokenize(s):
"""Separate string s into tokens. A token can be:
a comment (which is ignored); a paren or ,@ or , or quote or quasiquote; a non-string atom;
or a string consisting of quotes around (non-quotes or backslash plus anything)."""
tokens = re.findall(r"""\s*(;.*|,@|[('`,)]|[^\s('");]+|"(?:[\\].|[^\\"])*")\s*""", s)
return [t for t in tokens if not t.startswith(';')]In this case, what is the best option? to be faithful to the original sentence, or to stick to what sounds best with the parentheses inserted?
A similar problem comes up with capitalization in sentences beginning with a parenthesis. For example, in this:
(Luckily) Some are already there.
Capitalizing “Some” looks weird, even granted it’s a clumsy construction. But I think it’s probably best to do it that way.
PS. I just read my comment noticed I may have come off as a Grammar Nazi or something like that. It was an honest interest on the rules of composing sentences with parentheses (which I don't know myself).
this is a pretty standard exercise in university programming language theory courses. last year when i took the course, we had to write an oCaml interpreter in oCaml, and had about a week to do it (complete with environments, bindings, expressions, custom operators, higher-order functions, etc...). it was challenging, but paid off as far as contributing to my understanding about how a) functional languages work and b) how interpreters work. an interesting follow up would be to write a compiler for the language as well.
if writing an interpreter is something you've never done before then this is, by all means, a worthwhile activity. it would have been nice had it been structured as a series of descriptions and exercises rather than with the answers posted along with it. so easy to look :)
(HowTo (In (Write LispInterpreter) Python))
[if the title actually was LISP syntax, everybody would be programming in LISP today]
God has killed so many kittens because of you. ;-)
I've had a play this morning, and am already most of the way towards having the readline sitting in twisted and the instance feeding a webserver. Which is to say - lisp notation with all the benefit of python's great libraries, and I understand the layers completely.
No kittens were killed.
Besides, piggybacking off the existing Lisp implementation doesn't help much when you're trying to figure out how the infrastructure (GC, etc.) is implemented. I learned a LOT implementing a Scheme interpreter in OCaml a couple years ago, and wouldn't discourage anyone from doing so in whatever language they feel most comfortable with. (I'm actually taking a break from writing a small compiler right now, for a proprietary database query language we need to convert.)
I think you are/were mostly being voted down because of your tone.
(I was amazed when I saw that he wrote the scheme interpreter in Java in order to learn java better: http://norvig.com/jscheme-design.html)
Edit: fix grammar.
OTOH, one gets a lopsided view of the language. I have been learning Java for the last 1.5 months, most of it hacking on ABCL. I have got a good understanding of the runtime system, reflection, class-loading, security and the type-system. But I still look up primitive stuff like string manipulation and file I/O operations.
I miss Lisp a lot, but Java makes up in platform what it lacks as a language. You can use class instrumentation libraries to generate and manipulate code at run time. It's well worth tolerating, at least until I know enough of it to wrap the libraries I need to use from ABCL.
Eclipse is nice once you ditch the mouse and install the Emacs bindings plugin.