Saturday, June 25, 2005

Day 2: cons, car and cdr and more on lists

The following is a list in Lisp:

("foo" "bar" "baz")

You can construct this list in several ways. One way is to type in:

(list "foo" "bar" "baz")

The other way to get the same list is supposed to be with cons like:

(cons "foo" "bar")

The book seems to indicate that it should evaluate to:

("foo" "bar")

But I get

("foo" . "bar")


There seems to be a difference between the output expected from the book and that which I get from LispWorks. The implementation of Lisp that I am using when on a Windows machine is LispWorks Personal Edition. Maybe this explains the difference.

Anyway, cons is symetrical with a combination of car and cdr functions:

(cons (car (list 1 2 3)) (cdr (list 1 2 3)))

evaluates to

(1 2 3)

because

(car (list 1 2 3)) => 1
(cdr (list 1 2 3)) => (2 3)

and cons stitches them back into a list.

first and rest seem to be synonyms for car and cdr.

Thursday, June 23, 2005

Day 1: Thoughts and notes on readings

I thought that I would start reading Common Lisp: A Gentle Introduction to Symbolic Computation. Before starting this blog I had learnt a little of lisp already so this blog won't start at the beginning of my push to learn Lisp. I have already obtained some familiarity with lists, the functions car and cdr, some of the mathematical functions and the "listener" from viewing the divx movies: Structure and Interpretation of Computer Programs, Video Lectures.

I think that one of the core concepts in lisp is "evaluation". Lisp runs in a loop: it reads input, evaluates and prints output. The evaluation part could be thought of as execution of code. It is then this process evaluation that is the key to understanding Lisp. The basics are
  • A number evaluates to a number. If you type in 1 and hit return the result of evaluation is printed as 1
  • A string is represented as characters enclosed in double quotes. If you type in "foobar" and hit return the result of evaluation is printed as "foobar
That is all well and good but things get interesting when you type in foo. You get an error. This seems to be because when lisp tries to evaluate foo it doesn't know what to do. It isn't a string or a number so lisp thinks it is a symbol. In this case lisp has not been told what that symbol represents so it shows an error. You can stop the error by telling lisp not to evaluate the symbol by quoting it. This means putting a ' in front.

The next biggest thing to understand about lisp is functions. At the same time you would need to know about lists because they look the same ie data and program code look the same in lisp. It seems to be easier to talk about functions first. A function is like:

(print "hello, World!")


when lisp evaluates that it prints out "Hello, World!" then prints out what (print "hello, World!") evaluates to which is also "Hello, World!". So you get two "Hello, World!"'s.
One might say list is like the following on paper:

(1 2 3)
("foo" "bar")

but when you type that in lisp tries to evaluate it as a function and says "illegal function name 1" or "illegal function name "foo"". Basically lisp assumes it is a function and evaluates it accordingly. Once again you can stop this error by telling lisp not to evaluate the list by quoting it by putting a ' in front.

'(1 2 3 4)

That is it for now. I have read about more in the book but I haven't made notes about cons car and cdr functions, some predicates and math operations and the assocation between symbol-name symbol-function and lambda. More on this later.

Why?

I have been trying to learn lisp for some time now. I have trouble staying with tasks that take longer than an evening because I keep getting distracted by Real Life(tm). I hope to make a post to this blog everyday detailing new progress. Call it a goal. Everyone says that you need goals in life and this is a smaller goal that seems more acheivable than the more nefarious original goal "Learn Lisp".