NAME __________KEY______________________

 

SCXT 350

Lab day script and first exercise set (modified)

 

You may turn this in at the end of lab today, or work on it later on if you prefer, so long as you turn it in by Wednesday, Feb. 11 (in class).

 

The following gives some steps to follow for Friday’s lab session together with several questions for you to answer (and space for you to answer them in):

 

1.         Log into your account (on ACADEMIA) and start up Allegro Common Lisp (see notes from Wednesday).

 

2.         Try typing a few arithmetic expressions such as (+ 2 3), (- 7 5), (+ 3 (* 4 5)).

 

3.         Work out how to type in the following expressions in LISP.  Once you have done them, write down the LISP for of the expressions to the right of the expression.  Please do not simplify.

 

            7 – 2                                        Lisp expression  (- 7 2)

 

            (10 –  ( 2 * 3))                         Lisp expression:  (- 10 (* 2 3))

 

            (3 + 1) / (3 – 1)                        Lisp expression: (/ (+ 3 1) (- 3 1))

 

 

4.         Remember that assignment statements in LISP are done using setf.  Try the following statements:

 

            (setf lst ‘(a b c d))

            (car lst)

            (car (cdr lst))

            (cadr lst)

            (cons ‘z lst)

 

            What is the value of lst after this last statement ?  It remains unchanged

                        (still (a b c d))

 

            (to change the value of lst, we would need to type (setf lst (cons ‘z lst)))

 

 

           

5.         Without using first, second, third, etc., write expressions to

 

            find the first item in the list lst:    ___(car lst)_____

 

            find the third item in the list lst:    ___(car (cdr (cdr lst)))

                        or (caddr lst)

 

            swap the first and second item in the list lst:   

 

                        (be careful:  this should work whatever the value of lst is.  To be sure that

                        your statement works, try doing (setf lst ‘(q r s t)) and make sure that

                        your statement still works).

 

            We’ll do this in three steps:

                        Step 1:  The list past the first two elements is (cdr (cdr lst))

                                    or simply (cddr lst)

                        Step 2:  We want to add the first element of the original list

                                    to the beginning of this list (the list just created in

                                    step 1).  cons is the function we want

                                    to use to build the list, and (as above) the first item in

                                    the original list is (car lst), so

 

                                                (cons (car lst) (cddr lst))

 

                        Step 3 :  We now need to add the second element of the original

                                    list (which is (car (cdr lst)) or, more simply, (cadr lst)) to

                                    the start of the list in step 2:

 

                                    (cons (cadr lst) (cons (car lst) (cddr lst)))

 

6.         Recall that we can make new LISP functions using defun as, for example

 

            (defun addit (a b c)

                        (+ a (+ b c)))

 

            Write a small function called addsub which works as follows:  (addsub i j k) will return i – j + k.  For example (addsub 5 2 3) will return 5 – 2 + 3 or 6.  Try your

            new function out, and, when you are happy with it, write it down here.

 

            (defun addsub (i j k)

                        (+ k (- i j)))

 


 

 

7.         Now try and combine problems 5 and 6 by writing a function (using defun) called swap  which will accept any list and which will return the list with the first two items in the list swapped.  For example, (swap ‘(a b c)) should return (b a c). When you are happy with your solution, write it below:

 

 

                        (defun swap (lst)

                                     (cons (cadr lst) (cons (car lst) (cddr lst))))

 

 

                        (notice that the argument to the swap function is a single

                        atom representing a list).

 

 

8.         Finally, recall the recursive definition of factorial

 

            fact(n) = 1 if n < 2, and = n*fact(n-1) otherwise

 

            and its implementation in a Lisp defun

 

                        (defun fact (n)

                                    (if (< n 2) 1

                                                (* n (fact (- n 1)))))

 

            Using this as a pattern, here is the definition of a function called fib (for Fibonacci):

 

            fib(n) = 1 if n < 3 and is fib(n-1)+fib(n-2) otherwise

 

            For example fib(1) = fib(2) = 1, fib(5) = 5, fib(10) = 55.

 

            Write a defun for fib.  When it looks like it is working, copy it below:

 

(defun fib (n)

               (if (< n 3) 1

                 (+ (fib (- n 1)) (fib (- n 2)))))