第三章學題 Ansi Common Lisp, Paul Graham

本文探讨了高级列表操作技术,包括但不限于列表表示法、元素计数、路径查找等实用功能的实现方式,并提供了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Show the following lists in box notation:

    (a)

(a b (c d))

    (b)

(a (b (c (d))))

    (c)

(((a b) c) d)

    (d)

(a (b . c) . d)

2. Write a version of union that preserves the order of the elements in the original lists:

> (new-union '(a b c) '(b a d))
(A B C D)

3. Define a function that takes a list and returns a list indicating the number of times each (eql) element appears, sorted from most common element to least common:

> (occurences '(a b a d a c d c a))
((A . 4) (C . 2) (D . 2) (B . 1))

4. Why does

(member '(a) '((a) (b)))

return nil?

5. Supose the function pos+ takes a list and returns a list of each element plus its position:

> (pos+ '(7 5 1 4))
(7 6 3 7)

Define this function using (a) recursion, (b) iteration, (c) mapcar.

6. After years of deliberation, a government commission has decided that lists should be represented by using cdr to point to the first element and the car to point to the rest of the list.  Define the government versions of the following functions:

    (a) cons

    (b) list

    (c) length (for lists)

    (d) member (for lists; no keywords)

7. Modify the program in Figure 3.6 to use fewer cons cells.  (Hint: Use dotted lists.)

(defun compress (x)
  (if (consp x)
      (compr (car x) 1 (cdr x))
      x))

(defun compr (elt n lst)
  (if (null lst)
      (list (n-elts elt n))
      (let ((next (car lst)))
        (if (eql next elt)
            (compr elt (+ n 1) (cdr lst))
            (cons (n-elts elt n)
                  (compr next 1 (cdr lst)))))))

(defun n-elts (elt n)
  (if (> n 1)
      (list n elt)
      elt))

8. Define a function that takes a list and prints it in dot notation:

> (showdots '(a b c))
(A . (B . (C . NIL)))
NIL

 9. Write a program to find the longest finite path through a network represented as in Section 3.15.

(defun shortest-path (start end net)
  (bfs end (list (list start)) net))

(defun bfs (end queue net)
  (if (null queue)
      nil
      (let ((path (car queue)))
        (let ((node (car path)))
          (if (eql node end)
              (reverse path)
              (bfs end
                   (append (cdr queue)
                           (new-paths path node net))
                   net))))))

(defun new-paths (path node net)
  (mapcar #'(lambda (n)
              (cons n path))
          (cdr (assoc node net))))

 The network may contain cycles.

 

 

 

 

 

1.

    (a)

   

    (b)

   

    (c)

   

     (d)

   

2.

recursive

(defun uunion (og sec)
  (if (null sec)
      og
      (if (member (car sec) og)
          (uunion og (cdr sec))
          (uunion (append og (list (car sec))) (cdr sec)))))

iterative

(defun uunion (og sec)
  (let ((rs (list (car og))))
    (dolist (obj (append og sec))
      (pushnew obj rs))
    (reverse rs)))

 3

(defun occur (ll)
  (let ((nu nil))
    (dolist (obj ll)
      (if (assoc obj nu)
          (setf (cdr (assoc obj nu))(+ 1 (cdr (assoc obj nu))))
          (push (cons obj 1) nu)))
    (sort nu #'> :key #'cdr)))

 4

member 用 eql, 會比較記憶體位置和没有記憶體的數字與没有記憶體的 abcd 字, eq 比純記憶體, equal 比印的一不一樣

(member '(a) '((a) (b)) :test #'equal)

5

iteration

(defun addadd (ll)
  (let ((nu nil)(temp 0))
    (dolist (obj ll)
      (setf nu (cons (+ temp obj) nu))
      (setf temp (+ temp 1)))
    (reverse nu)))

recursive

(defun addadd (ll)
  (adda ll 0))

(defun adda (ll what)
  (if (null ll)
      nil
      (cons (+ (car ll) what) (adda (cdr ll) (+ 1 what)))))

mapcar

(defun addadd (ll)
  (let ((tmp -1))
    (mapcar #'(lambda (x)
                (setf tmp (+ tmp 1))
                (+ x tmp))
             ll)))

 6

    (a) cons

(defun mycons (x y)
  (let ((tmp '(nil . nil)))
    (setf (car tmp) x
          (cdr tmp) y)
    tmp))

    (b) list

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/chuangpoyao/blog/50754

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值