.comment -*- Mode:TEXT; -*- .comment all types of objects .document OBJECT - Information about Lisp objects. .tag OBJECT Lesson OBJECT, Version 2 Kent Pitman, 12/5/80 revised by Victoria Pigman, 9/1/82 The things you type in and the things Lisp types back at you are printed representations for internal objects and structures. Different flavors of objects have different uses and different printed representations. This lesson will deal with some of the more common objects that Lisp knows about -- how to type them in, manipulate them, and get Lisp to type them back out. ATOMS and LISTS For the purposes of this course, Lisp has two basic kinds of objects: ATOMs and LISTs. An atom, like you probably learned in science class, is a very small thing. Words (or SYMBOLs as we call them in Lisp) and Numbers are examples of atoms. LISTs are collections of things -- either collections of atoms or collections of other lists. SYMBOLS SYMBOLs are just what they sound like -- they are symbolic names for things. For example, the following objects are symbols: FOO HOUSE ELECTRIC-OUTLET CAR Try typing in some of these SYMBOLs. (You will have to type a single-quote mark before the name as in 'FOO or 'HOUSE to keep Lisp from evaluating them. If you haven't read about evaluation, see Lesson EVAL at your earliest opportunity.) .try SYMBOLs can have funny characters in them, too. For instance, if you wanted to put a space in a SYMBOL's name, you could do it in either of two ways -- one is to use the magic character "/" which says the next character is to be taken non-specially. So you could type in the atom FOO BAR (with a space in it) by typing FOO/ BAR. The space in this atom name is said to be slashified. Another way to do the same thing is to use the magic character "|". This says that Lisp should gobble characters after the "|" until another "|" is seen and pretend there were slashes in front of all of the characters in between. So, |ABC DEF GHI| is the same as ABC/ DEF/ GHI Lisp will optimize the printing of these things to try to minimize the number of characters it has to print. Try typing in some of these examples and see what comes out: A ; Normal atom A a ; Note that Lisp normally makes everything uppercase /a ; But you can inhibit that /a/b ; This keeps both characters small |ab| ; So does this |ABC| ; And it knows when vertical bars were un-needed |A C| ; and when they aren't. Don't forget the single quote. .try You may have noticed something funny if you tried rubbing out the / ... If you type A/b you will rub out both the slash and the b. That's because Lisp treats the /b as one character. Even harder to understand perhaps, but useful on occasion, is that if you want to insert a rubout (it is a valid character, after all) into an atom name, you can say / Thus if you type a / un-intentionally, it may take you two rubouts to get rid of it. Also note that if you wanted to get a / into an atom, you wouldn't be able to because / is already reserved for that special meaning ... so you do the obvious thing: // ... The symbol // is a one-character symbol. The first slash is just a marker saying the next character is to be taken as a normal character. Similarly, to get vertical bar into an atom, you would say /| ... Try typing in some of these odd atom names: FOO//BAR |FOO//BAR| ; Two names for same symbol! FOO/|BAR |FOO/|BAR| ; Also two names for same symbol! foo/|bar |foo/|bar| ; These are DIFFERENT symbols. Don't forget the single quote. .try Here is a cute set of atom names to try... /a //a ///a ////a /////a ...etc (do you see the pattern?) Don't forget the single quote. .try FIXNUMS Numbers come in many flavors. There are integer numbers (like 5 or 30 or -17). Lisp calls these numbers FIXNUMs. Try typing in some FIXNUMs and see what gets typed back. Numbers have the nice property of evaluating to themselves so you won't have to use a quote mark. .try FLONUMS There are also ``floating point numbers'' or ``real numbers'' like Pi or 1/2 or the square root of 2. Lisp calls these numbers FLONUMs. It would take too much storage (actually, an infinite amount) to store these numbers exactly, so decimal approximations are used. For example, Pi might be stored as just "3.14159" but 1/2 could be stored as "0.5". FLONUMs must have digits before and after the decimal point. eg, 3.7 is a FLONUM but 3. is a FIXNUM. .try BIGNUMS Maclisp also has a special class of numbers that are like FIXNUMs but can grow to arbitrary size. Most languages do not support such a feature. Try typing in a number like 923423423842342978897483749274897492834223. This is in many ways similar to a fixnum, but it is much bigger than the average FIXNUM, so it has a special name -- BIGNUM. Any number bigger than about 34359738367 is called a BIGNUM. Try typing in some bignums. .try LISTS You can also have collections of objects. The main such type is called the LIST (it is actually constructed out of a more primitive structure called the CONS (short for 'construct') or PAIR (because it's a pairing of two objects). We will get to this shortly). LISTs are designated in their printed representation by parentheses. All the objects in a list need not be of the same type. Hence you can have nice homogeneous lists like (3 4 5 6) which is a list of fixnums or you can have lists of mixed types like (A B 3.0 7 C). You can even have lists of lists. For example, ((3 4 5 6) (A B 3.0 7 C)) is a list 2 long whose elements are the two lists that we used for examples of lists in the previous paragraph. Try typing in some lists. Remember to use the single-quote mark to inhibit evaluation of your lists as in '(A B C) or '(3 4 5). .try Figuring Out The Type Of Something Lisp has a function which will tell you what the type of an object is. It is called TYPEP. To give you a feel for its use, here are some examples of various inputs and what they will return: (TYPEP '3) ==> FIXNUM (TYPEP 'A) ==> SYMBOL (TYPEP '(A B C)) ==> LIST For the last of these, perhaps a better thing to return would be CONS or PAIR, since that's what we call the primitive object from which a LIST is made up. Run through a few trials with TYPEP so you get the feel of it. We'll see in later lessons that it can be a very powerful tool in building programs work on varying kinds of input. .try There is a special kind of list called the empty list. It has the unique feature that it is both an atom and a list. This is because it is a list, but a very small one -- so small that you can't get any smaller -- so it has the unique honor of being an honorary atom. (In Maclisp, TYPEP will return SYMBOL for this object's type, but that is prone to be different in different dialects of Lisp.) Various Lisps have different syntax restrictions on the empty list. In Maclisp, the tokens NIL and () are exactly equivalent and both denote the empty list. Try typing in () and NIL and see what Lisp types back. .try .next DOT