CS 3323 Topics in Programming Languages


Lisp Assignment 1, Spring 1999
Due January 29, 1999

For this and later assignments you can use Sun Common Lisp on ringer or any other Lisp system you have access to.

  1. Try out Lisp on your account by logging in and typing
    		% lisp
    
    to activate lisp, and
    		> (quit)
    
    to quit. (The (exit) in the book does not work.)
  2. Try out numeric computations. Do
                    > (setq a 4)
                    > (setq b 5)
    
    and calculate
    		a^2 + b^2
    		sqrt(a^2 + b^2)
    		(4/3) pi a^3 
    
  3. Try
    		> (setq x1 0)
    		> (setq y1 0)
    		> (setq x2 1)
    		> (setq y2 0)
    		> (setq x3 -1)
    		> (setq y3 1)
    
    Use setq to assign
    		a = sqrt((x2 - x1)^2 + (y2 -y1)^2)
    
    and similarly for b and c, to give lengths of the sides of a triangle. Then set
    		alpha = arccos( (b^2 + c^2 - a^2) / (2bc)).
    
    and similarly for beta and gamma, to give the three angles of the triangle. Finally set
    		sum = alpha + beta + gamma
    		sumdeg = (180/p) sum
    
  4. Consider the follwing lisp function:
     
    		(defun factorial (n)
    		     (cond ((= n 0) 1)
    		           (t  (* n (factorial (1- n))))
    		     )
    		)
    
    Save this function as a file named "FACT.L". Then try out
    		> (load 'fact.l)
    		> (factorial 10)
    		> (factorial 20)
    
    Now try out the examples on pages 345-346 using time and compile. Do they work on your system?
  5. Try out car, cdr, cons, etc., using examples from 2.4, 2.5, .26 and 2.7 of your text. Try out
    		> (setq x '(a b))
    		> (setq y '(a b c))
    
    Now try (car x), (car y), (cdr x), (cdr y), (car (cdr x)), (car (cdry)), (cadr x), (cadr y). Try (cons x y), (append x y), (cons (car y) (cdr y)), etc.
  6. Try out append and list:
     
    		> (list 'a 'b 'c)
    		> (list 'a '(b c) 'd)
    		> (append '(a b) '(c d) '(e f))
    		> (setq l '(a b))
    		> (append l l)
    		> (append l l l)
    		> (list l l)
    		> (list l l l)
    		> (list 'l l)
    		> (append 'l l)          [an error]
    		> (append '(a) '() '(b) '())
    		> (append '((a) (b)) '((c)(d)))
    		> (list '((a)(b)) '((c)(d)))
    		> (cons l l)
    		> (cons 'l l)
    		> (length '(a (b c) d))
    		> (reverse '(a (b c) d))
    		> (car '(cdr '(a b c)))
    		> (car (cdr '(a b c)))
    		> (cons 'a nil)
    		> (setq x '(a b))
    		> (cons (car x) (cons (cadr x) '(c d)))
    
  7. Mimic the factorial function above to define a function harmonic, where (harmonic n) returns the value
    			1 + (1/2) + (1/3) + . . . + (1/(n-1)) + (1/n).
    
    1. First use the operator / with integers, so that the answers are rational numbers. [Try out n = 10, 100. As a check, the value of (harmonic 10) should be 7381/2520.]
    2. Then rewrite the harmonic function using constants with a decimal point to force Lisp to use floating point numbers. [Now as a check the value of (harmonic 10) should be 2.9289682539682538.]
    3. It turns out that (harmonic n) is approximately equal to ln(n) + g, where g = 0.5772156649. . . is known as Euler's constant. Check the accuracy of this approximation for n = 10, 100, 1000. [The accuracy should be better than 1/n.]
  8. Write a recursive function fibonacci that will return the nth fibonacci number F[n], where
    			F[0] = 0, F[1] = 1, and F[n] = F[n-1] + F[n-2].
    
    [For reference, (fibonacci 10) should return 55, (fibonacci 20) returns 6765 after a few seconds, and (fibonacci 30) returns 832040 after as much as several minutes. Why do you think this takes so long? Try (compile 'fibonacci) and (fibonacci 30) to see how long it takes.]
  9. The following interactive session shows how to create a log of a Lisp session for printing (so that you can turn in this assignment):
                    > 12
                    12
                    > (dribble 'file.out)  ; turn on logging
                    ;;; Dribble file "FILE.OUT" started
                    T
                    > (+ 3 4 )
                    7
                    > (dribble)            ; turn off loggin
                    ;;; Dribble file "FILE.OUT" finished
                    T
                    > (quit)
                    runner% cat FILE.OUT
                    ;;; Dribble file "FILE.OUT" started
                    T
                    > (+ 3 4 )
                    7
                    > (dribble)
                    ;;; Dribble file "FILE.OUT" finished
    


Revision Date: 1/20/99