Association of Lisp Users: ALU
Online tutorials: Successful Lisp:
How to Understand and Use Common Lisp
Lisp
Tutorial by Marc Schwarz
Lisp
Primer by Colin Allen and Maneesh Dhagat
Elementary lisp book by David S. Touretzky: .ps, .pdf [1 MB].
Elementary lisp book by David Cooper: .pdf, local .pdf [404 KB].
Guy Steele's lisp reference book (probably too hard): .ps, .pdf [4.6 MB].
Here are initial examples of functions in Lisp: .txt, .pdf, .ps.
Here is a way you could do the first part of this assignment more easily: .txt, .pdf, .ps.
% lisp > 12 12 > (dribble 'file.out) ; turn on logging ;;; Dribble file "FILE.OUT" started T > (+ 3 4 ) 7 > (dribble) ; turn off logging ;;; Dribble file "FILE.OUT" finished T > (quit) % cat FILE.OUT ;;; Dribble file "FILE.OUT" started T > (+ 3 4 ) 7 > (dribble) ;;; Dribble file "FILE.OUT" finished
Basics:
% lispto activate lisp, and
> (quit)to quit. (The commonly used (exit) does not work.)
> (setq a 4) > (setq b 5)and calculate
a^2 + b^2 sqrt(a^2 + b^2) (4/3)*pi*a^3
> (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) / (2*b*c)).and similarly for beta and gamma, to give the three angles of the triangle. Finally set
sum = alpha + beta + gamma sumdeg = (180/pi)*sum
> (setq x '(a b)) > (setq y '(a b c))Now try
> (car x), > (car y), > (cdr x), > (cdr y), > (car (cdr x)), > (car (cdr y)), > (cadr x), > (cadr y). > (cons x y), > (append x y), > (cons (car y) (cdr y))
> (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)))
Functions:
(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) > (factorial 50)
1 + (1/2) + (1/3) + . . . + (1/(n-1)) + (1/n).
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?]
(reverse '(a b c)) (reverse '((a b c))) (reverse '(a (b c d) e))Define your own function reverse1 which will behave the same way as reverse, reversing the order of elements of a list at the top level only. [Hint: Invoke reverse1 recursively on the cdr and tack on the car at the end.]