ANSI Common Lisp Exercises
ANSI Common Lisp Exercises
lisp
Exercises
Online-read Address:ansi Common Lisp
- ANSI Common Lisp Exercises
- Chapter 2 Exercises
- Chapter 3 Exercises
Chapter 2 Exercises
1.
(a) (+ (-5 1) (+ 3 7)) ==> 14
(b) (List 1 (+ 2 3)) ==> (1 5)
(c) (if (LISTP 1) (+ 1 2) (+ 3 4)) ==> 7
(d) (List (and (LISTP 3) t) (+ 1 2)) ==> (Nil 3)
2.
(cons ‘a ‘(b c))(cons ‘a (cons ‘b ‘(c)))(cons ‘a (cons ‘b (cons ‘c nil)))
3.
(defun my-four (lst) (if (listp lst) (car (cdr (cdr (cdr lst)))) nil))
4.
(defun gearter (a b) (if (and (numberp a) (numberp b)) (if (> a b) a b) nil))
5.
(a) The incoming argument needs to be a list to determine if there is a nil value in the list
(b) The first is a value, the second is a list, and the position of the first value in the list is calculated
6.
(a) car
(b) or
(c) Apply
7.
Recursive version
(defun list-in-list (lst) (if (not (null lst)) (let ((elt (car lst))) (if (listp elt) T (list-in-list (cdr lst)))) nil))
Iteration Version
(defun list-in-list (lst) (if (not (null lst)) (let ((ret nil)) (dolist (obj lst) (setf ret (or ret (listp obj)))) ret) nil))
8.
(a)
Recursive version
(defun print-point (num) (if (integerp num) (if (zerop num) nil (progn (format t ".") (print-point (- num 1)))) nil))
Iteration Version
(defun print-point (num) (if (integerp num) (do ((i 0 (+ i 1))) ((> i num) nil) (format t ".")) nil))
(b)
Recursive version
(defun n-times (n lst) (if (listp lst) (if (zerop (length lst)) 0 (if (eql n (car lst)) (+ (n-times n (cdr lst)) 1) (n-times n (cdr lst)))) nil))
Iteration Version
(defun n-times (n lst) (if (listp lst) (let ((len 0)) (dolist (obj lst) (if (eql obj n) (setf len (+ len 1)) nil)) len) nil))
9.
(a)
(SETF lst (remove nil lst))
The main reason is that remove does not modify the LST and needs to be re-assigned in order to implement the modification
(defun summit (lst) (setf lst (remove nil)) (apply #‘+ lst))
(b)
Because of the particularity of the list, it will cause the original program to enter a dead loop, unable to end
(defun summit (lst) (let ((x (car lst))) (if (null x) (if (zerop (length (cdr lst))) 0 (summit (cdr lst))) (+ x (summit (cdr lst))))))
(defun summit (lst) (if (zerop (length lst)) 0 (let ((x (car lst))) (if (null x) (summit (cdr lst)) (+ x (summit (cdr lst)))))))
Chapter 3 Exercises
1.
(a)
(cons ‘a (cons ‘b (cons (cons ‘c (cons ‘d nil)) nil)))
(b)
(cons ‘a (cons (cons ‘b (cons (cons ‘c (cons (cons ‘d nil) nil)) nil)) nil))
(c)
(cons (cons (cons ‘a (cons ‘b nil)) (cons ‘c nil)) (cons ‘d nil))
(d)
(cons ‘a (cons (cons ‘b ‘c) (cons ‘d nil)))
2.
(defun new-union (lst-a lst-b) (if (not (consp lst-b)) lst-a (new-union (if (if-union lst-a (car lst-b)) (append lst-a (list (car lst-b))) lst-a) (cdr lst-b))))(defun if-union (lst a) (if (consp lst) (if (eql (car lst) a) nil (if-union (cdr lst) a)) t))
3, (half product semi-finished, basically can get results, but did not sort the results )
(defun occurrences (lst) (if (consp lst) (append (list (repeat (car lst) lst)) (occurrences (remove (car lst) lst))) nil))(defun repeat (a lst) (if (consp lst) (let ((count 0)) (dolist (obj lst) (if (eql obj a) (setf count (+ count 1)) nil)) (cons a count)) nil))
4.
Because member uses EQL to compare objects, the main differences between EQL and equal are as follows:
(EQL ' a ' a) = = (equal ' a ' a) = = T
(EQL ' (a) ' (a))! = (equal ' (a) ' (a))
Personal Understanding:
EQL are equal only if the same object values are compared. The range of equal is wider than EQL, i.e. EQL returns T, then equal also returns T, and if equal returns T,EQL it may not return T.
5.
ANSI Common Lisp exercises (not finished)