% lispto activate lisp, and
> (quit)to quit. (The (exit) in the book 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) / (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
(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?
> (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.
> (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)))
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? Try (compile 'fibonacci) and (fibonacci 30) to see how long it takes.]
> 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