Chapter 1: Introduction.

 

            This tutorial introduces lisp languages. Lisp was invented by John McCarthy. Lisp stands for List Processor. It is an interpretive language. Statements written at the prompts are executed by the interpreter. Let us write two simple Lisp functions.

 

(defun square (num) (* num num))

 

This function computes the square of a number. ‘defun’ is used to define a function. The syntax of defun is .

(defun function-name (parameters ) function-body)

square’ is the name of the function. ‘num’ is the parameter. ‘(* num num)’ is the function body that finds the square of the ‘num’. The function can be called by writing

(square 3).

which computes the square of 3 and will print the output 9. The console looks like this.

[2] CL-USER(7): (defun square (x) (* x x))

SQUARE

[2] CL-USER(8): (square 3)

9

            The other lisp function is

 

(defun helloworld () (write "Hello world"))

 

The console looks like that:

[2] CL-USER(9): (defun helloworld () (write "Hello world"))

HELLOWORLD

[3] CL-USER(11): (helloworld)

"Hello world"

"Hello world"

 

The function is called by writing (helloworld).

                           Among various applications includes store like Yahoo Store, simplified English checker for the Boeing Company. LISP is also used in uncommon applications such as cancer radiation therapy planning.

                        LISP is also used to compute simple mathematical expressions. For example

[3] CL-USER(12): (+ 4 5)

9

[3] CL-USER(13): (* (+ 4 2 ) 4)

24

[3] CL-USER(14):

 

 

The most common type of objects used in lisp are atoms and lists. Evaluator always finds the value for the atom.

           The function setq is used to assign values to the function.

 

CG-USER(5): (setq var1 10)

10

CG-USER(6): var1

10

CG-USER(7):

 

 

The interpreter consider list as having function name followed by argument lists.

The function cons allow to add values at the front of the list. Another way to create the list is to use the list function.

 

 

[1] CG-USER(15): (list 3 4 5 6)

(3 4 5 6)

[1] CG-USER(16): (setq a (list 2 3 4 5))

(2 3 4 5)

[1] CG-USER(17):

 

The functions car, cdr, first, rest is used for obtaining the elements of the lists.

 

1] CG-USER(17): (car a)

2

[1] CG-USER(18): (Cdr a)

(3 4 5)

[1] CG-USER(19):

 

The function car returns the first element of the list and function cdr returns the rest of the elements of the list. The combination of car and cdr can be used for various purposes.

 

The function car of cdr a returns the second element of the list. Try getting the third element of the list a using car and cdr.(Exercise)

 

The functions first and rest are used to obtain first and rest of the elements of the list.

 

[1] CG-USER(21): (first a)

2

[1] CG-USER(22): (rest a)

(3 4 5)

[1] CG-USER(23): (first (rest a))

3

[1] CG-USER(24):

 

 

The following are variants of lisp car and cdr.

 
(caar x)        (car (car x))                    
(cadr x)        (car (cdr x))                    
(cdar x)        (cdr (car x))                    
(cddr x)        (cdr (cdr x))                    
(caaar x)       (car (car (car x)))              
(caadr x)       (car (car (cdr x)))              
(cadar x)       (car (cdr (car x)))              
(caddr x)       (car (cdr (cdr x)))              
(cdaar x)       (cdr (car (car x)))              
(cdadr x)       (cdr (car (cdr x)))              
(cddar x)       (cdr (cdr (car x)))              
(cdddr x)       (cdr (cdr (cdr x)))              
(caaaar x)      (car (car (car (car x))))        
(caaadr x)      (car (car (car (cdr x))))        
(caadar x)      (car (car (cdr (car x))))        
(caaddr x)      (car (car (cdr (cdr x))))        
(cadaar x)      (car (cdr (car (car x))))        
(cadadr x)      (car (cdr (car (cdr x))))        
(caddar x)      (car (cdr (cdr (car x))))        
(cadddr x)      (car (cdr (cdr (cdr x))))        
(cdaaar x)      (cdr (car (car (car x))))        
(cdaadr x)      (cdr (car (car (cdr x))))        
(cdadar x)      (cdr (car (cdr (car x))))        
(cdaddr x)      (cdr (car (cdr (cdr x))))        
(cddaar x)      (cdr (cdr (car (car x))))        
(cddadr x)      (cdr (cdr (car (cdr x))))        
(cdddar x)      (cdr (cdr (cdr (car x))))        

(cddddr x)                 (cdr (cdr (cdr (cdr x))))     

 

Try using these functions in lisp (Exercise).

The function length is used to obtain the length of the list.

 

[1] CG-USER(26): (length a)

4

[1] CG-USER(27):

 

The function append is used to append list to another list.

 

[1] CG-USER(30): (append (list 3 4 5) a)

(3 4 5 2 3 4 5)

[1] CG-USER(31): (append a (list 3 4 5))

(2 3 4 5 3 4 5)

[1] CG-USER(32):

 

The function setf can also be used to assign values to variables but only existing one

 

 

The function print is used to print the values on the screen. The function read is used to read the input.

 

[1] CG-USER(34):  (print "hello world")

 

"hello world"

"hello world"

[1] CG-USER(35): (print 1)

 

1

1

[1] CG-USER(36):

 

[1] CG-USER(36): (Setq a (read))

34

 

34

[1] CG-USER(37): a

34

[1] CG-USER(38):

 

 

The details of this chapter will be presented in I/O chapter.

 

 

Exercises

  1. Write lisp code for the following expression.
    1. 3 + 4 + 5 + 6 + 7
    2. (3+5 ) * ( 4+5))
    3. 4 /  ( 4 + 3 )
  2. Write a function that will add 2 to any number.
  3. Write lisp function for the following.
    1. A + B + (C / 2) +D

      4.  Express(CADR x) and (CADDR x) in terms of CAR, CDR and expression x.