Python syntax in Lisp and Scheme

Peter Seibel peter at javamonkey.com
Fri Oct 3 18:13:27 EDT 2003


jcb at iteris.com (MetalOne) writes:

> kalath at lycos.com (Mark Brady) wrote in message news:<e840346c.0310030302.6be0c378 at posting.google.com>...
> > Personally I find Scheme and Common Lisp easier to read but that's
> > just me, I prefer S-exps ...
> 
> I am just barely familiar with Lisp and Scheme. However, I always
> find comments like the above interesting. I have seen other people
> make this claim also. However, from an earlier post on
> comp.lang.python comparing a simple loop.
> 
> Scheme
> (define vector-fill!
>   (lambda (v x)
>     (let ((n (vector-length v)))
>       (do ((i 0 (+ i 1)))
>           ((= i n))
>           (vector-set! v i x)))))
> 
> Python
> def vector_fill(v, x):
>     for i in range(len(v)):
>         v[i] = x
> 
> To me the Python code is easier to read, and I can't possibly fathom
> how somebody could think the Scheme code is easier to read. It truly
> boggles my mind.

Well, over in comp.lang.lisp (where we speak Common Lisp, more so than
Scheme) we might write that:

  (defun vector-fill (v x)
    (dotimes (i (length v)) (setf (aref v i) x)))

or 

  (defun vector-fill (v x)
    (loop for i below (length v)
          do (setf (aref v i) x)))  (defun vector-fill (v x)


which seems pretty similar to the Python version.

(If of course we didn't already have the FILL function that does just
that.)

> The second thing that puzzles me is the usage of the LISP macro
> system. This system is touted as one of LISPs major strengths. I
> believe the "Do" above is a macro. Is that the best syntax that can
> be achieved with a macro for "Do".

Nope. Common Lisp includes the standard macros DOTIMES and LOOP as
shown above.

> I would think there would already be macros to write the Scheme code
> above in a format similar to the Python code below, or some more
> readable syntax. I have looked for repositories of such macros and I
> can't find any.

I'm sure the Scheme folks will tell you were you can find such
repositories for Scheme. But Common Lisp has them built in.

> This leads me to think that in practice LISP macros are not used.

That is decidedly not true of Common Lisp.

> Couple this with the fact that LISP programmers seem happier with
> S-exprs, and I can't see why a LISP programmer would even want to
> write a macro.

Well, macros aren't intended to get you away from s-exps though the
LOOP macro does to a certain extent. They are just intended to get you
to more to-the-point s-exps. You put your finger right on it when you
wondered why there wasn't a better way to write your loop than DO. If
DOTIMES didn't already exist, you'd write it as a macro that expands
into DO. That is: DO is a almost completely general looping construct
which makes it more than you want in a lot of situations. Macros let
you turn what you want to write (DOTIMES) into the right version of
the more general, more powerful construct (DO).

> I have tried on 3 occassions to become a LISP programmer, based upon
> the constant touting of LISP as a more powerful language and that
> ultimately S-exprs are a better syntax. Each time, I have been
> stopped because the S-expr syntax makes we want to vomit.

Hmmm. If all three of those times have been with Scheme, you might
want to try Common Lisp for a change of pace.

> If a set of macros could be written to improve LISP syntax, then I
> think that might be an amazing thing.  An interesting question to me
> is why hasn't this already been done.

Some (including me) would argue it has. They just don't define
"improve" as "get rid of all sexps".

-Peter

-- 
Peter Seibel                                      peter at javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp




More information about the Python-list mailing list