Lisp Books

Showing posts with label lists. Show all posts
Showing posts with label lists. Show all posts

Monday, 5 April 2010

Lists in Lisp


Lists are are one of Lisp's most flexible and powerful data types. Here are some examples of lists:
(red orange yellow green blue indigo violet)
(10 green bottles)
(9 18 27 36 45 54 63 72 81 90 99 108)
(thisisalist)
((a)(list of) (lists))

To get the length of a list, we can use the LENGTH function. We have to put the quote mark ' in front of each word here, otherwise Lisp would try to evaluate or understand what we mean:

length '(red orange yellow green blue indigo violet))
7

(length '(10 green bottles))
3

(length '(9 18 27 36 45 54 63 72 81 90 99 108))
12

(length '(thisisalist))
1

(length '((a)(list of) (lists)))
3

As you can see from the final example, lists can contain other lists. The elements of the list are the things that appear inside only one level of parentheses, so in the final example the LENGTH function returns 3 because there are only 3 elements to the list:

(a)
(list of)
(lists)


As we saw briefly in the first steps with Lisp post we can extract individual elements from lists using these functions:


(first '(a b c d e))
A

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

(first (first '((a)(list of) (lists))))
A

Creating a new list is very easy using the LIST function:
(list 'banana)
(banana) 

(list 'water 'malt 'hops 'yeast)
(WATER MALT HOPS YEAST)


Again we have to put the quote mark ' in front of each word here, otherwise Lisp would try to evaluate or understand what we mean by water, malt, hops and yeast i.e. had we assigned values to these.

We'll look more at using, creating and extracting data from lists in future posts.

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