shouchengcheng
just do it

ANSI Common lisp Exercises

 
 

ANSI Common lisp Exercises

lisp Exercises


Online-read address: ANSI Common lisp

 

Chapter 2 习题

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)传入的实参需要是list,判断list中是否有nil值
(b)第一个是值,第二个是list,计算list中第一个值的位置

6、
(a)car
(b)or
(c)apply

7、
递归版本

(defun list-in-list (lst)
    (if (not (null lst))
        (let ((elt (car lst)))
            (if (listp elt)
                T (list-in-list (cdr lst))))
            nil))

迭代版本

(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)
递归版本

(defun print-point (num)
    (if (integerp num)
        (if (zerop num)
            nil
            (progn (format t ".") (print-point (- num 1))))
        nil))

迭代版本

(defun print-point (num)
    (if (integerp num)
        (do ((i 0 (+ i 1)))
        ((> i num) nil)
        (format t "."))
        nil))

(b)
递归版本

(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))

迭代版本

(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))
主要是因为remove是不会对lst进行修改的,需要通过重新赋值的方式才可以实现修改

(defun summit (lst)
    (setf lst (remove nil))
    (apply #'+ lst))

(b)
由于list的特殊性,会导致原来的程序会进入一个死循环之中,无法结束

(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 习题

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 半成品,基本能够得到结果,但是没有对结果进行排序)

(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、
因为member使用eql来比较物件,eql和equal的主要区别如下所示:
(eql 'a 'a) == (equal 'a 'a) == T
(eql '(a) '(a)) != (equal '(a) '(a))
个人理解:
eql只能对同一个对象值进行比较时才相等。而equal的范围比eql宽泛,即eql返回T,那么equal也返回T;如果equal返回T,eql未必返回T。

5、

 

posted on 2014-12-27 12:53  shouchengcheng  阅读(280)  评论(0编辑  收藏  举报