CS 3723
 Programming Languages 
   Lisp Functions  


Example Lisp Functions
% cat length1.l
(defun length1 (list)
     (cond ((null list) 0)
           (t (+ 1 (length1 (cdr list)))) ))
> (length1 ())
0
> (length1 nil)
0
> (length1 '(a b c))
3
> (length1 '((a b) c (d e (f))))
3

% cat myfind.l (defun myfind (word sent) (cond ((null sent) 'not-found) ((equal word (car sent)) 'found) (t (myfind word (cdr sent))) )) > (myfind 'is '(now is the time)) FOUND > (myfind 'for '(now is the time)) NOT-FOUND > (myfind '(a) '(a b c)) NOT-FOUND > (myfind '(b) '(a (b) c)) FOUND > (myfind '(c (d e)) '((a b) (c (d e)) e (f))) FOUND
% cat add.l (defun add (list) (cond ((null list) 0) (t (+ (car list) (add (cdr list)))) )) > (add '(2)) 2 > (add '(2 3)) 5 > (add '(2 3 4)) 9 > (add nil) 0 > (add 3) >>Error: The value of X, 3, should be a LIST > (add '(2 (3 4))) >>Error: The value of NUMBER1, (3 4), should be a NUMBER
% cat add-all.l (defun add-all (list) (cond ((null list) 0) ((atom (car list)) (+ (car list) (add-all (cdr list)))) (t (+ (add-all (car list)) (add-all (cdr list)))) )) > (add-all '(1)) 1 > (add-all '(2 3)) 5 > (add-all '(3 4 5)) 12 > (add-all '(2 (3 4))) 9 > (add-all '( ( (2 3) 4) ( 1 5))) 15
% cat list-atoms.l (defun list-atoms (list) (cond ((null list) nil) ((atom (car list)) (cons (car list) (list-atoms (cdr list)))) (t (append (list-atoms (car list)) (list-atoms (cdr list)))) )) > (list-atoms ()) NIL > (list-atoms '(a)) (A) > (list-atoms '(a b)) (A B) > (list-atoms '(a (b c))) (A B C) > (list-atoms '((a b) c (d (e (g) f)))) (A B C D E G F) > (list-atoms '(() a)) (NIL A) *** NOT CORRECT ANSWER *** > (list-atoms (car '(()a))) NIL > (list-atoms (cdr '(() a))) (A) > (append nil '(a)) (A) > (atom (car '(() a))) T > (atom ()) T *** OK, HERE'S THE PROBLEM *** > (cons (car '(() a)) (list-atoms (cdr '(() a)))) (NIL A)
% cat list-atoms.l (defun list-atoms (list) (cond ((null list) nil) ((null (car list)) (list-atoms (cdr list))) ((atom (car list)) (cons (car list) (list-atoms (cdr list)))) (t (append (list-atoms (car list)) (list-atoms (cdr list)))) )) > (list-atoms ()) NIL > (list-atoms '(a)) (A) > (list-atoms '(a (b c))) (A B C) > (list-atoms '((a b) c (d (e (g) f)))) (A B C D E G F) > (list-atoms '(() a b)) (A B) > (list-atoms '(() a)) (A) > (list-atoms '(nil (a nil ()) () b nil ())) (A B)
% cat list-atoms.l (defun list-atoms (list) (cond ((null list) nil) ((and (not (null (car list))) (atom (car list))) (cons (car list) (list-atoms (cdr list)))) (t (append (list-atoms (car list)) (list-atoms (cdr list)))) )) > ; note: how append and cons handle nil (append nil '(a) nil) (A) > (cons 'a nil) (A) > (list-atoms '((a b) c (d (e (g) f)))) (A B C D E G F) > (list-atoms '(nil (a nil ()) () b nil ())) (A B)

  • Define a function count-atoms that will count the number of atoms in a list, at all levels. For example (count-atoms '(a (b c) (d (e) f))) should return 6. [Hint: Mimic list-atoms from class.]
  • Write a ``super'' reverse function srev which reverses all lists that occur in an S-expression at any level. Thus (srev '((a b (c d)) e (f g))) should yield ((g f) e ((d c) b a)). [Hint: This is similar to the previous reverse1, except for additional recursion.]

    Revision date: 2013-03-03. (Please use ISO 8601, the International Standard Date and Time Notation.)