第二章學題 Ansi Common Lisp, Paul Graham

1. Describe what happens when the following expressions are evaluated:shell

    (a) express

(+ (- 5 1) (+ 3 7))

    (b) app

(list 1 (+ 2 3))

    (c) this

(if (listp 1) (+ 1 2) (+ 3 4))

    (d) lua

(list (and (listp 3) t) (+ 1 2))

2. Give three distinct cons expressions that return (a b c).code

3. Using car and cdr, define a function to return the fourth element of a list.orm

4. Define a function that takes two arguments and returns the greater of the two.three

5. What do these functions do?element

    (a) rem

(defun enigma (x)
  (and (not (null x))
       (or (null (car x))
           (enigma (cdr x)))))

    (b)

(defun mystery (x y)
  (if (null y)
      nil
      (if (eql (car y) x)
      0
      (let ((z (mystery x (cdr y))))
        (and z (+ z 1))))))

 6. What could occur in place of the x in each of the following exchanges?

    (a)

> (car (x (cdr '(a (b c) d))))
B

    (b)

> (x 13 (/ 1 0))
13

    (c)

> (x #'list 1 nil)
(1)

7. Using only operators introduced in this chapter, define a function that takes a list as an argument and returns true if one of its elements is a list.

8. Give iterative and recursive definitions of a function that

    (a) takes a positive integer and prints that many dots.

    (b) takes a list and returns the number of times the symbol a occurs in it.

9. A friend is trying to write a function that returns the sum of all the non-nil elements in a list.  He has written two versions of this function, and neither of them work.  Explain what's wrong with each, and give a correct version.

    (a)

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

    (b)

(defun summit (lst)
  (let ((x (car lst)))
    (if (null x)
        (summit (cdr lst))
        (+ x (summit (cdr lst))))))

 

 

 

 

 

 

 

 

個人解

1(a)加減乘除(b)(1 5)(c)7(d)(NIL 3)

2

(cons 'a '(b c))
(cons 'a (cons 'b '(c)))

(cons 'a (cons 'b (cons 'c ())))

(cons 'a (cons 'b (cons 'c nil)))

3

(defun fourth (list)
  (car (cdr (cdr (cdr list)))))

4

(defun equalnot (a b)
  (if (> a b)
      a
      (if (< a b)
          b
          (format t "same~%"))))

5

    (a)查 list 是否空,且裏有無 nil

    (b)查 y 有否 x,給位置

6

    (a)car

    (b)or

    (c)apply

7

(defun check (ll)
  (if (eql nil ll)
      nil
      (if (listp (car ll))
          t
          (check (cdr ll)))))

8

iterative

(a)

(defun prntDt (num)
  (do ((i 1 (+ i 1)))
      ((> i num) format t "~%")
    (format t ".")))

(b)

(defun fnda (ll)
  (let ((count 0))
    (dolist (obj ll)
      (if (eql obj 'a)
          (setf count (+ count 1))))
    count))

recursive

(a)

(defun prntDt (num)
  (if (eql num 0)
     (format t "~%")
     (progn
       (format t ".")
       (prnt (- num 1)))))

(b)

(defun fnda (ll)
  (if (null ll)
      0
      (if (eql 'a (car ll))
          (+ 1 (fnda (cdr ll)))
          (fnda (cdr ll)))))

9

(a)

(defun summit (ll)
  (progn        #這使你接下來可用多 list
    (remove nil ll)
    (apply #'+ ll)))

(defun summit (ll)     
  (apply #'+ (remove nil list)))   #馬上用, 不怕

(defun summit (ll)
  (setf ll (remove nil ll))    #setf -> 撤底改了
  (apply #'+ ll))

(b) 須要判斷是否空 list

相關文章
相關標籤/搜索