Home > Archive > Functional > May 2007 > The Merits of the Head of Expression Inside vs Outside the Parenthesis
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
The Merits of the Head of Expression Inside vs Outside the Parenthesis
|
|
| Xah Lee 2007-05-11, 10:04 pm |
| The Merits of the Head of Expression Inside vs Outside the
Parenthesis
Xah Lee. 2007-05
Lisp's nested parenthesis syntax is a Functional Notation. It has the
general form of =E2=80=9C(f a b ...)=E2=80=9D where any of the symbols insi=
de the
matching parenthesis may again be that form. For example, here's a
typical code from Emacs Lisp.
(defun fold (f x li)
"Recursively apply (f x i), where i is the ith element in the list
li.\n
For example, (fold f x '(1 2)) computes (f (f x 1) 2)"
(let ((li2 li) (ele) (x2 x))
(while (setq ele (pop li2))
(setq x2 (funcall f x2 ele))
)
x2
)
)
Vast majority of computer languages, interpret source code in a one-
dimensional, linear nature. Namely, from left to right, line by line,
as in written text. (Examples of computer languages's source code
that
are not linear in nature, are spread sheets, cellular automata,
graphical programing languages) For languages that interprets source
code linearly, the logics of their syntax necessarily have a
hierarchical structure (i.e. a tree). The lisp's notation, is the
most
effective in visually showing the logics of the syntax. This is
because, a function and its arguments, are simply laid out inside a
parenthesis. The level of nesting corresponds to the =E2=80=9Cprecedence=E2=
=80=9D in
evaluating the expression.
The first element inside the matching parenthesis, is called the
=E2=80=9Chead=E2=80=9D of the expression. For example, in =E2=80=9C(f a b)=
=E2=80=9D, the =E2=80=9Cf=E2=80=9D is the
head. The head is a function, and the rest of the symbols inside the
matching parenthesis are its arguments.
The head of lisp's notation needs not to be defined as the first
element inside the parenthesis. For example, we can define the =E2=80=9Chea=
d=E2=80=9D
to be the last element inside the parenthesis. So, we write =E2=80=9C(arg1
arg2 ... f)=E2=80=9D instead of the usual =E2=80=9C(f arg1 arg2 ...)=E2=80=
=9D and its
syntactical analysis remains unchanged. Like wise, you can move the
head outside of the parenthesis.
In Mathematica, the head is placed in front of the parenthesis, and
square brackets are used instead of parenthesis for the enclosing
delimiter. For example, lisp's =E2=80=9C(f a b c)=E2=80=9D is syntactically
equivalent
to Mathematica's =E2=80=9Cf[a,b,c]=E2=80=9D. Other examples: =E2=80=9C(sin =
=CE=B8)=E2=80=9D vs =E2=80=9CSin[=CE=B8]=E2=80=9D,
=E2=80=9C(map f list)=E2=80=9D vs =E2=80=9CMap[f,list]=E2=80=9D. Placing th=
e head in front of the
matching bracket makes the notation more familiar, because it is a
conventional math notation.
However, there is a di vantage in moving the head of a expression
from inside the matching bracket to outside. Namely: The nesting of
the matching delimiters, no longer corresponds to the logics of the
syntax, when the head is itself a compound expression.
For example, suppose Reflection(vectorV,pointP) is function that
returns a function f, such that f(graphicsData) will reflect the
graphicsData along a line passing pointP and parallel to vectorV. In
lisp, we would write =E2=80=9C((Reflection vectorV pointP) graphicsData)=E2=
=80=9D. In
Mathematica, we would write =E2=80=9CReflection[vectorV,pointP]
[graphicsData]=E2=80=9D. In lisp's version, the nesting corresponds to the
logics of the evaluation. In the Mathematica's form, that is no
longer
so.
For another example, suppose Deriv is a function that takes a
function
f and returns a function g, and we want to apply g to a variable x.
In
lisp, we would write =E2=80=9C((Deriv f) x)=E2=80=9D. In Mathematica, we wo=
uld write
=E2=80=9CDeriv[f][x]=E2=80=9D. In lisp's version, the nesting corresponds t=
o the
logics of the evaluation. In the Mathematica's form, the logics of
the
evaluation no longer corresponds to the nesting level, because now
the
head is outside of the enclosing delimiters, so the head of
expressions no longer nests. The merit of lisp's =E2=80=9C(f x)=E2=80=9D fo=
rm versus
Mathematica's =E2=80=9Cf(x)=E2=80=9D form.
------------
This post is excerpted from
=E2=80=9CThe Concepts and Confusions of Prefix, Infix, postfix and Fully
Functional Notations=E2=80=9D, archived at
http://xahlee.org/UnixResource_dir/writ/notations.html
Xah
x...@xahlee.org
=E2=88=91 http://xahlee.org/
| |
| Jon Harrop 2007-05-12, 4:14 am |
| Xah Lee wrote:
> For another example, suppose Deriv is a function that takes a
> function
> f and returns a function g, and we want to apply g to a variable x.
> In
> lisp, we would write ?((Deriv f) x)?. In Mathematica, we would write
> ?Deriv[f][x]?.
It is probably worth noting that currying is comparatively uncommon in both
Lisp and Mathematica. The difference between the syntaxes is fairly small:
((f a) b) Lisp
f[a][b] Mathematica
f a b ML
--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/produc...journal/?usenet
|
|
|
|
|