
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































((simone jones)numbers, date of birth, diagnosis, primary physician. A patient record could be structured as a list consisting of a name, address, telephone (angela a aardvark md))(pruritis-of-the-palm)(10 11 1978)((home 206-234-5678) (work 425-432-8765))((1234 boylston street) seattle wa 98102) ...(defun city (pt) (second (address pt)))(defun street-address (pt) (first (address pt)))(defun address (pt) (second pt))(defun family-name (pt) (second (name pt)))(defun first-name (pt) (first (name pt))) (defun name (pt) (first patient)) For each item, we define an accessor
(defun phone-number (pt &optional type) (let ((numbers (third pt))) (second (if type (assoc type numbers))
(first numbers))))
(defun print-nice-date (day month year)(defun birth-date (pt) (fourth pt)) (format nil "˜A-˜A-˜A" day (month-name month) year))
(defun month-name (m) (elt ’("JAN" "FEB" "MAR" "APR" "MAY" "JUN"
(1- m)))
(defun formatted-birth-date (pt) (let ((date (birth-date pt)))
(print-nice-date (first date) (second date) (third date))))
If find
(^) were not part of Common Lisp, we could write it. If
(^) thing
(^) is in the list,
(^) junk
, the
function returns it. Otherwise it returns
(^) nil
.
(defun my-find (thing junk) (cond ((null junk) nil)
(t (my-find thing (rest junk)))))((eql thing (first junk)) thing)
(defun my-find (thing junkHow could it be generalized? How could this be improved?
&key (test #’eql)
(key #’identity))
(cond ((null junk) nil)
((funcall test thing
(funcall key (first junk)))
(t (my-find thing (rest junk)(first junk))
:test test :key key))))
(defun my-reverse (s) (if s (append (my-reverse (rest s))
(list (first s)))))
(defun another-reverse (s);;;----------------------------------------
(reverse-iter s
nil))
(defun reverse-iter (s accum) (if (null s) accum (reverse-iter (rest s)
(cons (first s) accum))))
In (^) C (^) you need a “main” to use it interactively.
{ main() #include <stdio.h> printf("n! = %d\n" factorial(n))scanf("%d", &n)int n;
{ long factorial(int n)/* for any small integer n, returns n! */} for (i = 1; ip = 1;int i, p;
<= n; ++i)
p = p *
i;
return p;
Here is how the factorial function would look in
(^) java
(^) (taken from “Java in a Nutshell”, by
/**David Flanagan, with end of line comments omitted):
This program computes factorial of a number
public class Factorial { public static void main(String[] args) { double result =int input = Integer.parseInt(args[0]);
(^) factorial(input);
System.out.println(result);
public static double factorial(int x) {} if (x < 0) return 0.0;
while(x >double fact = 1.0;
fact = fact *
(^) x;
x = x -
return fact;}
senting bases, An example of a procedure to map a DNA strand (represented by a list of symbols repre-
(defun dna-copy (input) (if (null input) nil (cons (dna-comp (first input))
(dna-copy (rest input)))))
The (^) dna-comp
(^) function just changes A to T and G to C, and vice versa.
(defun dna-comp (x) (cond ((eql x ’g) ’c)
(t (error "your dna is screwy"))))((eql x ’t) ’a)((eql x ’a) ’t)((eql x ’c) ’g)
Sometimes you would like to apply a predicate like
(^) and
(^) or (^) or , to another predicate func-
won’t work because (apply #’and (mapcar #’numberp my-list))tion on a list of items, e.g. to to check if every element of a list is a number, but
(^) and
(^) is a macro, not a function.
(defun check-if-all-numbers (list)Instead you could just use recursion to handle this. (if (null list) t (and (numberp (first list))
(check-if-all-numbers (rest list)))))
This pattern occurs so often that several functions were added to Common Lisp:
(^) every
some
, notany
, and
(^) notevery
. Just as with
(^) mapcar
(^) you could define your own version
(defun my-every (fn list)if it were not included. (if (null list) t (and (funcall fn (first list))
(my-every fn (rest list)))))
patient-list)family-name
(first (remove-if-not #’(lambda (x)
(and (eql first-name
(first-name x))
(eql family-name
(family-name x))))
patient-list))))