Sunday, 21 March 2010

First Steps With Lisp

Assuming you've followed yesterday's post and installed a version of Common Lisp, today we'll look at some first steps in learning Lisp.

We'll discuss background ideas and other useful theory in future posts, but for now we'll dive straight in and get our hands dirty with some Lisp programming.

1. Open your Lisp listener window, if it's not all ready showing, look for 'Listener' under your window menu.

2. As you would expect, Lisp can perform all sorts of mathematical operations, type this into your listener:
(+ 2 3)

It should of course return 5. There are a couple of useful points here:
-The function always come first
-The function and its arguments are surrounded by ()

3. Here are some more examples:
(+ 2 4 5 6)
17

(* 10 9)
90

(- 10 2.5)
7.5

and so on...
4. These operations can be nested inside one another for example:
(+ 1 (* 3 3))
10
The inner parentheses get evaluated first.

5. Aside from mathematical functions, many function access data. For example:
(first '(a b c d e))
A

6. The function 'first' returns the first item of a list. There are many other similar functions for accessing data:
(second '(a b c d e))
B

(third '(a b c d e))
C

(rest '(a b c d e))
(B C D E)

(last '(a b c d e))
(E)

7. The ' quote mark stops the Lisp from trying to evaluate the list (from trying to interpret what the list means - we could have for example assigned values to these letters).

Tomorrow we'll start building our own functions, but for now get used to using these functions in your version of Lisp