From: Xah
Subject: Re: trees, indexes, and expressions
Date: 
Message-ID: <6e7j70$llo$1@nntp2.ba.best.com>
I think I'm beginning to get some idea on why the level spec isn't needed in
lisp programing.

I think the reason is because the language differences. Mma programing tends
to build up lots of inert expressions, while Scheme or lisp's expresion is
more active, thus, the Level is more useful in mma programing.

In mma, every symbol or new symbol is equivalent to a quoted symbol of
Scheme, unless the symbol is defined. Symbols take on a meaning (and will
act like
operators) after they are defined (if they are), otherwise they just sit
there as a wrapper or atom. Because of this, mma programing tends to have
lots of structures to manipulate, which makes the level spec argument
useful.

For example, mma's Times[Plus[a,b],c] is like Scheme's (* (+ 'a 'b) 'c) if
a,b,c do not evaluate to numbers (not technically correct). When symbols are
not defined, they act like quoted. This leads to lots of structures.

--

Here's an example of how the level spec is useful in mma.

Suppose Polygon is an object that represensts a polygon in space, as used in
 (Polygon (List a1 a2 a3) (List b1 b2 b3) (List c1 c2 c3))

where each (List n1 n2 n3) represents the vertex of the polygon. Now I may
have a polyhedra or surface represented as

 (define myPolyhedra
  (List
   (List (Polygon ...) (Polygon...) ...)
   (List (Polygon ...) (Polygon...) ...)
  ...
  )
 )

Polygons are grouped by the their symmetry or other purposes.

Now suppose I want to translate myPolyhedra by the vector (List 1 1 1). This
can be done as:

(Map translateFunction myPolyhedra (LevelSpec -2))

This construct will apply translateFunction to all (List n1 n2 n3) in
myPolyhedra since they are at the -2 level of myPolyhedra.

Now suppose if I want to shrink each polygon by 90% with respect to their
centroid. I may write a function shrinkPolygon that takes a (Polygon...) as
an argument and returns the shrinked polygon, and I can then apply
shrinkPolygon to myPolyhedra using

(Map shrinkPolygon myPolyhedra (LevelSpec -3)) or
(Map shrinkPolygon myPolyhedra (LevelSpec 2))

Note that myPolyhedra can also be defined as

 (define myPolyhedra
  (List
   (List (List (List n1 n2 n3)...))
   (List (List (List n1 n2 n3)...))
  ...
  )
 )

so that methods relying on checking heads of subexpressions are not always
an alternative.

 Xah ยทยทยท@best.com
 http://www.best.com/~xah/SpecialPlaneCurves_dir/specialPlaneCurves.html
 "The eighth word in this sentence is true."   --Logician of Wasamata U.