Lisp Books

Wednesday 24 March 2010

Using Cons To Create Lists In Lisp

Today's post looks at creating lists in Lisp. Before we do that let's have a look at the answer to yesterday's post:

Write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.

(defun square-odd-p (x)
   (oddp (* x x)))


Creating Lists in Lisp
cons is a Common Lisp primitive, that creates lists from an atom
(number or letter) and a list:

(cons 1 '(2 3))
(1 2 3)


 We'll use cons to help us write a function to replace the first member of a list. In order to do this we'll:
  • Have to take two inputs to our function: the new item and the list
  • We'll use the rest function to return everything but the first member of the list
  • We can then cons our new item to the start of this list:
(defun replace-first (new-item the-list)
   (cons new-item (rest the-list)))


And an example:

(replace-first 'peter-pan '(one flew over the cuckoos nest))
   (PETER-PAN FLEW OVER THE CUCKOOS NEST)


In the next post we'll look at some more examples of building and manipulating lists.

Tuesday 23 March 2010

Predicates In Lisp

Today we're looking at predicates. Predicates are tests that we can use to see if something is true or false.

Before we do that let's look at the answers from yesterday:

(defun minus-one (x)
   (- x 1))

(defun ten-times-bigger (x)
   (* x 10))

(defun weeks-to-days (weeks)
   (* weeks 7))

It doesn't matter if you called weeks x or something entirely different, it is often useful to give our arguments names that are useful.

Predicates
As mentioned earlier predicates are tests that return either True or False. In lisp:

T = True
Nil = False

Here are some examples:

oddp checks to see if a number is odd:

(oddp 5)
T

(oddp 6)
NIL

evenp checks to see if a number is even:

(evenp 5)
NIL

(evenp 6)
T

Here are some other common predicates with examples:
(zerop 0)
T

(= 3 4)
NIL

(> 3 4)
NIL

(< 5 10)
T



Predicate Summary
  • (evenp n) true if n is even
  • (floatp n) true if n is a floating point number
  • (integerp n) true if n is an integer
  • (minusp n) true if n is less than zero
  • (numberp n) true if n is a number 
  • (oddp n) true if n is an odd number (0 is considered even) 
  • (plusp n) true if n is greater than 0 
  • (zerop n) true if n is 0
  • (> x y) true if x is greater than y
  • (< x y) true if x is less than y
  • (>= x y) true if x is greater than or equal to y
  • (<= x y) true if x is less than or equal to y
Using Predicates In Functions
In yesterday's post we wrote our own functions. We can include predicates in these functions. For example, this function adds two numbers together and checks to see if the result is even, again we could all it almost anything but as it's a predicate that check to see if the sum of two numbers is even we'll call it sum-even-p:

(defun sum-even-p (x y)
   (evenp (+ x y)))



Notice the order of the function

    (evenp (+ x y)))

the input element to evenp is the + function and x y


Today's Challenge
Today's challenge write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.

Check tomorrow's post for the answer and more Lisp tutorials.

Monday 22 March 2010

Writing A Function In Lisp

There are a large number of built in functions in Lisp, it's also possible, useful and very easy to write your own functions in Lisp. Have a look at this function:

(defun my-square (x)
(* x x))

Let's break this down:

defun is a function used to define new functions.
my-square is the name that we've given to our function, we could call it almost anything we like but it makes to give it a useful name so that we can remember it later.
x is the argument or input to our function. Again we could call this almost anything we like.
(* x x) This is where the actual work of our function takes place, we using the multiply function and multiplying x by itself.

When you have written and evaluated this function, it can be called and used just any other function:
(my-square 5)
25

(my-square 9)
81

It's good practice to comment your function, both so that others can understand it quickly and easily and for yourself.

(defun my-square (x)
 "this function squares an input number x"
(* x x))

Using double quotes " " adds a definition to your function, these can only be used after you have defined the input arguments.

A more versatile way of adding comments that can be used anywhere is to use semicolons, anything after the semicolon is ignored:

(defun my-square (x)
 "this function squares an input number x"

(* x x)) ; this line squares x

We'll work more on writing new functions tomorrow, for now see if you can write these functions, answers in tomorrow's post:

minus-one a function that takes a single number as input and returns the number that is one less
ten-times-bigger a function that takes a single number as input and returns the number that is ten times bigger
weeks-to-days a function that takes a number of weeks as input and returns the total number of equivalent days

These examples may seem simple, however this is a really good stepping stone to learn how to build much more powerful functions in Lisp. Answers and more on building functions tomorrow...

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

Saturday 20 March 2010

Learning Lisp

Lisp (an acronym for List Processing) is a powerful programming language designed to create other computer programs. The most used form of Lisp today is Common Lisp, it's available on every computer platform and uniform in use through a standard reference manual.

To start programming with LISP you need to install an integrated development environment (IDE). The good news is that there are many Common Lisp IDE's available for free:

Allegro Common Lisp a free version is available for Linux, Windows, Mac OSX and FreeBSD
Armed Bear Common Lisp (ABCL) a free open source implementation for Linux, Windows, Mac OSX and FreeBSD. Needs to be compiled before installing so possibly not the best choice for beginners.
CLISP available for Linux, Max OS X, Windows, xBSD, Solaris, Tru64, Solaris, HP-UX, BeOS, NeXTStep, IRIX, AIX
Lispworks a full commercial version and a free 'personal edition' are available for Macintosh, Linux, Windows and FreeBSD.
SBCL Steel Bank Common Lisp, a free open source implementation available for Linux, OSX, FreeBSD and with a version 'in progress' for Windows.

There is also a very good Lisp plugin called CUSP available for the Eclipse programming environment.

Choose and install a Lisp IDE for you platform and follow these Lisp tutorials and exercises to start learning Common Lisp today.