The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Saturday, April 08, 2006

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

I'm tempted to read that paragraph as:

The preceding chapter discussed the list, Lisp's most versatile 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 flexible 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

But I'm better than that.

Yes, structures in Common Lisp are a bit easier than in guile:

(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:

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 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.

Obligatory Picture

[The future's so bright, I gotta wear shades]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: https://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

https://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.