Where is quote (again)?

Jason Orendorff jason at jorendorff.com
Fri Mar 8 13:25:16 EST 2002


Gerhard Häring wrote:
> Le 08/03/02 à 07:21, N Becker écrivit:
> > Back in 1999, I asked "where is quote?".  I was looking for
> > a way to quote a function (delay evaluation).
> > [...]
>
> Just out of interest: what's that good for?

I think N Becker is asking for two different things.

1.  Quoting.  For example, suppose the character $ had
the effect of "quoting" the following Python expression.
Just as "" marks turns something into a string, the $
turns it into a parse tree.

Here's how you might use it.

  def sortlist(lst, code = $cmp(a, b)):
      cmp_function = lambda a, b: eval(code)
      lst.sort(cmp_function)

  x = [...]
  sortlist(x, $(cmp(a.name, b.name) or cmp(b.date, a.date)))

In Lisp, the apostrophe ' is the quote character.

Quoting is important in languages where you can extend
the syntax yourself.  Lisp is such a language, and it
uses quoting to implement features like (delay) and (force)
below.

2.  Lazy evaluation.  This is easier to describe, I guess. :)
Laziness is not evaluating something until you actually need
the result.

xrange() is lazy.  range() is eager.
Generators are lazy.  List comprehensions are eager.

You can use laziness to handle huge or infinite amounts of
information.  Think of the typical generator example, which
calculates Fibonacci numbers.  Obviously you can't calculate
that sequence eagerly; it's infinite.

Scheme has language features (delay) and (force) that support
laziness.  SICP (Abelson, Sussman, Sussman) discusses them in
the context of "streams", which are a sort of cousin of
iterators (different philosophy though).
  http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html
By the way, I recommend reading this book from page 1 onward.
Lisp doesn't subscribe to "Flat is better than nested";
this book will teach you the opposite way of thinking.

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list