* * * * *
I think Forth is a higher level language than Lisp
> The preceding chapter discussed the list, Lisp's most versatile data
> structure. This chapter show how to use Lisp's other data structures:
> arrays (including vectors and arrays), structures, and hash tables. They
> may not be as flexible as lists, but they can make access faster, and take
> up less space.
>
> “Chapter 4, _ANSI Common Lisp_ [1]”
>
I'm tempted to read that paragraph as:
> The preceding chapter discussed the list, Lisp's [DELETED-most versatile-
> DELETED] only data structure. This chapter show how to use Lisp's other
> badly implemented data structures: arrays (including vectors and arrays),
> structures, and hash tables. They may not be as [DELETED-flexible-DELETED]
> slow as lists, but they can make access faster and at the same time, more
> tedious, and take up less space.
>
> “Chapter 4, _ANSI Common Lisp_ [2]”
>
But I'm better than that.
Yes, structures in Common Lisp are a bit easier than in guile [3]:
> (defstruct foo
> x
> y)
>
You have the name of the structure and each field. The (defstruct) function,
however, will go ahead and make several functions for you to help “manage”
the structure, such as (make-foo), (foo-p) (foo-x) and (foo-y).
Yes, by declaring a structure, one ends up with a function per field, plus
two additional functions. So one goes:
> (defstruct foo
> x
> y)
>
> (setf a (make-foo))
> (setf (foo-x a) 42)
> (setf (foo-y a) 12)
>
Which to me doesn't make any sense. And to describe why it doesn't make
sense, I have to go into some depth about Common Lisp symbols. A symbol in
Common Lisp is what most people would consider a name. In (setf x 42) both
setf and x are symbols. But internally symbols are just more than a name. Way
more.
A symbol is also a type unto itself in Common Lisp (as in Scheme, but there,
they're way simpler) and contain more than just its name:
Table: Layout of the Common Lisp Symbol foo
Name foo
Value 42
Function (some internal pointer to code)
There are a few other fields, like which package it's in, and property lists
(a series of name/value pairs, much like Common Lisp's associated lists, but
aren't stored or implemented as associated lists—go figure) that aren't
germane to this discussion, so they're left out.
But notice that each symbol has both a “function” and a “value.” That means
one can define setf to have a value, like:
> (setq setf 42)
>
A parenthetical aside: “But wait a second,” you say, “didn't you use (setf)
above to set the value?” Yes, but one can also use (setq) to make sure one
sets the “value” of a symbol to a given value. (setf) is now preferred over
(setq) because it's more generalized:
> (setf {place value}*
> A generalization of setq: stores in the locaiton associated with
> each place the value of the corresponding value expression. If a
> value expression refers to one of the preceding places, it will get
> the new value. Returns the value of the last value.
>
> A valid place expression may be: a variable; a call to any function
> designated here as “settable” so long as the relevant argument is a
> valid place; a call to apply whose first argument is #'aref, #'bit,
> or #'sbit; a call to an accessor function defined by defstruct; a
> the or values expression in which the argument(s) are valid places;
> a call to an operator for which a setf expansion has been defined;
> or a macro call that expands into any of the preceding.
>
>
Hey! I didn't write this crazy language! And thus ends the parenthetical
aside.
Now, given that each symbol can have both code and data associated with it,
why one couldn't have done:
> (defstruct foo
> x
> y)
>
> (foo a)
> (setf (a x) 42)
> (setf (a y) 12)
>
Something like that is the first think a programmer in Forth (yet another
language where one has to implement common data structures by hand, yet it
never claimed to be a HLL (Higher Level Language) to begin with, and one can
write a Forth compiler in about 1k of code, so it's a very simple langauge).
But no, we get a profusion of “special functions” created for each structure.
And heaven help you if you create a structure with a field named “p”!
Oh, what does (foo-p) do?
By Common Lisp convention, functions that end in “p” or “-p” are queries,
such as (string-greaterp) (is one string greater than another?) or (equalp)
(one of the five or so equality functions) or (complexp) (is a value a
complex number?) so in this case, (foo-p) is asking if the value is a
structure of type foo (and since I don't have a Common Lisp environment, I
can't see what happens if one does create a structure with a field named
“p”).
Yeah, I'm really begining to agree with John McCarthy about Lisp [4].
[1]
http://www.amazon.com/exec/obidos/ASIN/0133708756/conmanlaborat-20
[2]
http://www.amazon.com/exec/obidos/ASIN/0133708756/conmanlaborat-20
[3]
gopher://gopher.conman.org/0Phlog:2006/04/05.1
[4]
gopher://gopher.conman.org/0Phlog:2006/01/25.1
Email author at
[email protected]