Why don't people like lisp?
myfirstname.mylastname at jpl.nasa.gov
Wed Oct 22 07:11:11 CEST 2003
In article <8Nnlb.619$I04.380 at newsread4.news.pas.earthlink.net>, "Andrew
Dalke" <adalke at mindspring.com> wrote:
> > Python has the ability to do exactly
> > what you're saying (domain language -> AST -> Python code or AST ->
> > compiler). It's rarely needed (I've used it twice now in my six years
> > or so of Python), so why should a language cater to make that
> > easy at the expense of making frequent things harder?
> As an example, here's a quick hack of a way to parse a simple
> stack-based language and make a native Python function out
> of it. I am the first to admit that it's ugly code, but it does work.
> I am curious to see the equivalent code in Lisp.
Here's a quick and dirty version:
(defvar *operations* '(+ - * / **))
(defun ** (n e) (expt n e))
(defun rpn-compile (expr)
(let ( stack free-vars )
(dolist (term expr)
(cond ( (numberp term) (push term stack) )
( (member term *operations*)
(push (list term (pop stack) (pop stack)) stack)
(rotatef (second (first stack)) (third (first stack))) )
(t (push term stack)
(pushnew term free-vars))))
(unless (= (length stack) 1) (error "Syntax error"))
(eval `(lambda (&key , at free-vars) ,(first stack)))))
? (funcall (rpn-compile '(0 b - b 2 ** 4 a c * * - 0.5 ** + 2 a * /))
:a 1 :b 6 :c 3)
More information about the Python-list