Flat-schmat! [was Re: Python syntax in Lisp and Scheme]

Andrew Dalke adalke at mindspring.com
Sat Oct 4 19:11:43 EDT 2003


Still have only made slight headway into learning Lisp since the
last discussion, so I've been staying out of this one.  But

Kenny Tilton:
> Take a look at the quadratic formula. Is that flat? Not. Of course
> Python allows nested math (hey, how come!), but non-mathematical
> computations are usually trees, too.

Since the quadratic formula yields two results, I expect most
people write it more like

droot = sqrt(b*b-4*a*c)  # square root of the discriminate
x_plus   = (-b + droot) / (4*a*c)
x_minus = (-b - droot) / (4*a*c)

possibly using a temp variable for the 4*a*c term, for a
slight bit better performance.

> It occurred to me that, if the computation
> is indeed the moral equivalent of the quadratic formula, calling various
> lower-level functions instead of arithmetic operators, then it is
> /worse/ to be reading a flattened version in which subexpression results
> are pulled into local variable, because then one has to mentally
> decipher the actual hierarchical computation from the bogus flat sequence.

But isn't that flattening *exactly* what occurs in math.  Let me pull
out my absolute favorite math textbook - Bartle's "The Elements
of Real Analysis", 2nd ed.

I opened to page 213, which is in the middle of the book.

   29.1  Definition.  If P is a partition of J, then a Riemann-Stieltjes sum
of f with respect to g and corresponding to P = (x_0, x_1, ..., x_n) is a
real
number S(P; f, g) of the form

                           n
     S(P; f, g) = SIGMA  f(eta_k){g(x_k) - g(x_{k-1})}
                         k = 1

Here we have selected number eta_k satisying

  x_{k-1} <= eta_k <= x_k for k = 1, 2, ..., n

There's quite a bit going on here behind the scenes which are
the same flattening you talk about.  For examples: the definiton
of "partition" is given elsewhere, the notations of what f and g
mean, and the nomenclature "SIGMA"


Let's try mathematical physics, so I pulled out Arfken's
"Mathematical Methods for Physicists", 3rd ed.

About 1/3rd of the way through the book this time, p 399

Exercise 7.1.1  The function f(z) expanded in a Laurent series exhibits
a pole of order m at z = z_0.  Show that the coefficient of (z-z_0)**-1,
a_{-1}, is given by
                      1          d[m-1]
  a_{-1} = ------- * ------------- * (  (z-z_0)**m * f(z)) evalutated as  )
                  (m-1)!      d z [m-1]
x -> x_0

This requires going back to get the definition of a Laurent series,
and of a pole, knowing how to evaluate a function at a limit point,
and remembering the bits of notation which are so hard to express
in 2D ASCII.  (the d[m-1]/dz[m-1] is meant to be the d/dz operator
taken m-1 times).

In both cases, the equations are flattened.  They aren't pure trees
nor are they absolutely flat.  Instead, names are used to represent
certain ideas -- that is, flatten them.  Yes, it requires people to
figure out what these names mean, but on the other hand, that's part
of training.

And part of that training is knowing which terms are important
enough to name, and the balance between using using old
names and symbols and creating new ones.

> So if we have:
>
> (defun some-vital-result (x y z)
>     (finally-decide
>        (if (serious-concern x)
>            (just-worry-about x z)
>           (whole-nine-yards x
>              (composite-concern y z)))))
>
> ...well, /that/ visually conveys the structure of the algorithm, almost
> as well as a flowchart (as well if one is accustomed to reading Lisp).
> Unwinding that into an artificial flattening /hides/ the structure.

"Flat is better than nested." does not mean nested is always and
forever wrong. "Better" means there's a balance.

"Readability counts." is another guideline.

> I do not know what Zen is, but I do now that is not Zen.

The foreigner came to the monestary, to learn more of the
ways of Zen.  He listened to the monks then sat cross-legged
for a day, in the manner of initiates.  Afterwards he complained
to the master saying that it would be impossible for him to reach
Nirvana because of the pains in his legs and back.  Replied the
master, "try using a comfy chair," but the foreigner returned
home to his bed.

The Zen of Python outlines a set of guidelines.  They are not
orthogonal and there are tensions between them.  You can
take one to an extreme but the others suffer.  That balance
is different for different languages.  You judge the Zen of Python
using the Zen of Lisp.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list