merits of Lisp vs Python

Robert Uhl eadmund42 at NOSPAMgmail.com
Tue Dec 12 12:30:40 EST 2006


"HowiPepper" <hpepper at gmail.com> writes:

> I have checked out Lisp several times in the past, but I always get
> turned off completely by the parenthesis you have to use for
> everything.  What's up with that anyway?

It's no different from having to use newlines and spaces and square
brackets and parentheses in Python.  E.g. the Lisp (f x y) is exactly
equivalent to the Python f(x, y); the Lisp (if x y z) is exactly
equivalent to the Python:

  if x:
    y
  else:
    z

There is, though, an advantage: (f x y) and (if x y z) aren't just parts
of programs--they're also lists.  That means that one can use all the
list-manipulation primitives on a program, and thus that it's very easy
to write programs which manipulate programs.  This in turn leads to
being able to extend the syntax of the language.  Imagine if one could
write this in Python:

  defsyntax unless(condition, commands):
    if not condition:
      commands

And use it like this:

  unless day == 'Sunday':
    work()

That'd be pretty cool, right?  After all, 'unless day is Sunday' conveys
the sense of what you're doing a little better than 'if days is not
Sunday.'  But in Python it's not possible--even my defsyntax example is
absurd: how would the compiler know the difference between pre-colon
argument and the command block?  What if I wanted multiple blocks
(e.g. as with an else:)?

Whereas with Lisp one might simply write:

  (defmacro unless (condition &body commands)
     `(if (not ,condition)
          , at commands))

When used as:

  (unless (equal day "Sunday")
          (work))

The compiler transforms it into:

  (if (not (equal day "Sunday"))
      (work))

This is a _really_ simple example (unless is nice, but it's hardly
vital); but what one can do is end up making a much more declarative
program by creating new syntax to describe stuff.

> With Python's ease of learning and use, availability of a large number
> of libraries, extremely active development community and large
> user-base, I'd say the question to ask is what specific advantages
> over Python does Lisp offer, to make people switch to it?

Well, these come to mind; they're in no order and are just my own
particular thoughts.  No doubt more reflective commentators can come up
with a better list.

o Macros

As mentioned above, macros can make one's life significantly nicer.  I
use Python a lot (it's currently a better choice than Lisp for many of
the problems I face), and I find myself missing macros all the time.
The ability to take some frequently-used idiom and wrap it up in a macro
is wonderful.  E.g. a common idiom in Python is:

  file = open(path, 'r')
  for line in file.readlines():
      foo(line)
      bar(line)
      baz(line)

Even this isn't much nicer:

  for line in open(path, 'r').readlines():
      foo(line)
      bar(line)
      baz(line)

Wouldn't it be nice to have a macro with-open-file?

  filefor line in path:
          foo(line)
          bar(line)
          baz(line)

o Speed

Lisp interpreters are several orders of magnitude faster than Python,
and Lisp compilers are faster yet.  Speed's not the most important
thing, but it is _an_ important thing; all other things being equal, the
faster solution is better.

o Symbols

In Lisp, a symbol is essentially a hashed string; two symbols are alike
if their names are alike, but comparison of symbols is a constant-time
operation.  Thus where in Python I have lots of string comparisons for
constants, and in C I have #defined integers, in Lisp I have symbols.
It's not just a performance hack--symbols are part of why macros and
packages work--but when I miss them, I miss them for the performance
side of things

o CLOS

The Common Lisp Object System is a really remarkable piece of work.
Among other things, it has generic functions instead of methods.
E.g. in Python or most other OO languages object.method(arg1, arg2) is
really just a fancy piece of syntactic sugar for method(object, arg1,
arg2); method does different things depending on the type of object, its
first argument.

Wouldn't it be nice to be able to specialise a method on _any_ subset of
its arguments, not just its first one?  Well, CLOS offers that.  (method
object1 object2) could be specialised on the first argument, the second
or both.  This can be very powerful.

Wouldn't it be nice to specify that some action be taken before or after
a superclass's method, rather than over-riding that method entirely?
Sure, one can over-ride the method and then call it within one's own
code, but that obscures the meaning of what one's doing.

o CLSQL

An OR mapper which actually works.  'Nuff said.


OTOH, here's what Python offers which Lisp doesn't:

o A capable standard library

Lisp's standard library was considered large once upon a time--why, it
included lists and hashes!  But that's not a big deal nowadays.  Right
now there are a plethora of choices, but it's not clear which choices
are blessed or particularly good.  Sure, once could use a different MIME
library than Python offers as standard, but it's good to know that there
_is_ a standard.

o A large, friendly community

Lisp's community is small and exceedingly bright; it doesn't suffer
fools gladly.  Python's is larger and friendlier, realising that we were
all fools once and that with education many of us get better.

o Top-notch Web frameworks

Pylons and Django are nice to use and take care of a lot of the
boilerplate one would otherwise have to write.  While Webactions is a
very cool framework, it lives at a lower level and there's a lot more
one has to add to it for a complete app.

-- 
Robert Uhl <http://public.xdi.org/=ruhl>
People who are willing to rely on the government to keep them safe are
pretty much standing on Darwin's mat, pounding on the door, screaming,
`Take me, take me!'                                     --Carl Jacobs



More information about the Python-list mailing list