在将函数格式与递归连接后,在Common Lisp中打印字符串

时间:2015-04-11 15:38:55

标签: common-lisp sbcl

我正在尝试从Paul Graham学习Common Lisp阅读Ansi Common Lisp并使用EEC325 course批判和运行测试功能以及讲座。我用粘液和SBCL设置了Emacs

问题在于第3章练习8说:

  

定义一个获取列表并以点表示法打印的函数:

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

我做了以下函数,结果是一个字符串,它适用于案例,但不打印哪个是练习的主要目标

(defun show-dots (lst)
  (cond
    ((atom lst) (format nil  "~A" lst ))
    ((consp lst) (format nil "(~A . ~A)"
             (show-dots (car lst))
             (show-dots (cdr lst))))))

问题是它产生的字符串不会打印字符串,但它可以正常工作

CS325-USER> (SHOW-DOTS '(A B C))
"(A . (B . (C . NIL)))"
CS325-USER>  (SHOW-DOTS '(A (B C)))
"(A . ((B . (C . NIL)) . NIL))"
CS325-USER>  (SHOW-DOTS '(A . B))
"(A . B)"
CS325-USER>  (SHOW-DOTS NIL)
"NIL"
CS325-USER>  (SHOW-DOTS '(NIL))
"(NIL . NIL)"

测试需要打印,所以它确实失败了,但显然打印结果时会出现问题

 (run-tests show-dots)
SHOW-DOTS: (SHOW-DOTS '(A B C)) failed: 
Should have printed "(A . (B . (C . NIL)))" but saw ""
SHOW-DOTS: (SHOW-DOTS '(A (B C))) failed: 
Should have printed "(A . ((B . (C . NIL)) . NIL))" but saw ""
SHOW-DOTS: (SHOW-DOTS '(A . B)) failed: 
Should have printed "(A . B)" but saw ""
SHOW-DOTS: (SHOW-DOTS NIL) failed: 
Should have printed "NIL" but saw ""
SHOW-DOTS: (SHOW-DOTS '(NIL)) failed: 
Should have printed "(NIL . NIL)" but saw ""
SHOW-DOTS: 0 assertions passed, 5 failed. 

所以我认为我必须要做的唯一事情是打印这个字符串,创建后但这些东西不起作用,我不明白为什么

1)尝试

(defun show-dots (lst)
  (cond
    ((atom lst) (format t  "~A" lst ))
    ((consp lst) (format t "(~A . ~A)"
             (show-dots (car lst))
             (show-dots (cdr lst))))))

有了这个结果

CS325-USER> (SHOW-DOTS '(A B C))
ABCNIL(NIL . NIL)(NIL . NIL)(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS '(A (B C)))
ABCNIL(NIL . NIL)(NIL . NIL)NIL(NIL . NIL)(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS '(A . B))
AB(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS NIL)
NIL
NIL
CS325-USER>  (SHOW-DOTS '(NIL))
NILNIL(NIL . NIL)
NIL

所以我说好的,这是疯狂的第一个字符串并打印它

(defun show-dots (lst)
 (format t (cond
    ((atom lst) (format nil  "~A" lst ))
    ((consp lst) (format nil "(~A . ~A)"
             (show-dots (car lst))
             (show-dots (cdr lst)))))))

但结果不正确

CS325-USER> (SHOW-DOTS '(A B C))
ABCNIL(NIL . NIL)(NIL . NIL)(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS '(A (B C)))
ABCNIL(NIL . NIL)(NIL . NIL)NIL(NIL . NIL)(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS '(A . B))
AB(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS NIL)
NIL
NIL
CS325-USER>  (SHOW-DOTS '(NIL))
NILNIL(NIL . NIL)
NIL

所以我说好了让我们创建一个局部变量放在那里并打印它,但它不能再次工作

(defun show-dots (lst)
  (let ((str (cond
         ((atom lst) (format nil  "~A" lst ))
         ((consp lst) (format nil "(~A . ~A)"
                  (show-dots (car lst))
                  (show-dots (cdr lst)))))))
    (format t str)))

,结果不正确

CS325-USER> (SHOW-DOTS '(A B C))
ABCNIL(NIL . NIL)(NIL . NIL)(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS '(A (B C)))
ABCNIL(NIL . NIL)(NIL . NIL)NIL(NIL . NIL)(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS '(A . B))
AB(NIL . NIL)
NIL
CS325-USER>  (SHOW-DOTS NIL)
NIL
NIL
CS325-USER>  (SHOW-DOTS '(NIL))
NILNIL(NIL . NIL)
NIL

所以我真的很想明白这里发生了什么,也许是愚蠢的事情,但我不明白这一点。

谢谢你的时间

3 个答案:

答案 0 :(得分:6)

生成字符串的原始函数实际上非常接近。问题是生成字符串的函数不应该打印字符串,如果它将被递归调用,因为你不希望打印中间字符串。您可以做的一个非常简单的更改是使 show-dots 函数的主体成为创建字符串的内部帮助函数,然后在main函数中,打印辅助函数的结果:

(defun show-dots (lst)
  (labels ((%show-dots (lst)
             (cond
               ((atom lst) (format nil  "~A" lst ))
               ((consp lst) (format nil "(~A . ~A)"
                                    (%show-dots (car lst))
                                    (%show-dots (cdr lst)))))))
    (write-string (%show-dots lst))
    nil))

CL-USER> (show-dots '(a b c))
(A . (B . (C . NIL)))
NIL

你可以这样做的另一种方法是使用一个可选参数来指示是否应该打印或返回字符串,它可以默认打印,但在递归情况下,你会返回它。实际上,由于格式将 t nil 作为具有这些语义的输出参数,因此您可以将其设置得非常狡猾:

(defun show-dots (lst &optional (output t))
  ;; If OUTPUT is T (the default) then stuff will actually be printed,
  ;; and FORMAT returns NIL.  If OUTPUT is NIL (as it is in the
  ;; recursive calls), then FORMAT creates the string and returns it,
  (cond
    ((atom lst) (format output "~A" lst))
    ((consp lst) (format output "(~A . ~A)"
                         (show-dots (car lst) nil)
                         (show-dots (cdr lst) nil)))))

CL-USER> (show-dots '(a b c))
(A . (B . (C . NIL)))
NIL

也就是说,这两个实现最终都创建了一堆中间字符串,然后将它们连接在一起。这不是对空间的有效利用。在遍历正在打印的对象时写入流可能会更好。也许最直接的方法是处理括号的格式并点自己。这会导致一个或多或少像这样的解决方案(它返回 nil ,因为这就是你给出的第一个例子):

(defun print-dotted (object &optional (stream *standard-output*))
  "Print the object as usual, unless it is a cons, in which case
always print it in dotted notation. Return NIL."
  (prog1 nil
    (cond
      ;; write non-conses with WRITE
      ((not (consp object))
       (write object :stream stream))
      ;; write the "(" " . " and ")" by hand,
      ;; and call print-dotted recursively for
      ;; the car and the cdr.
      (t (write-char #\( stream)
         (print-dotted (car object) stream)
         (write-string " . " stream)
         (print-dotted (cdr object) stream)
         (write-char #\) stream)))))

CL-USER> (print-dotted '(a b c))
(A . (B . (C . NIL)))
;=> NIL

现在,格式函数实际上能够使用tilde slash指令在格式字符串中命名时调用其他函数。这意味着你可以做这样的事情,我觉得它很优雅(我定义了一个新的包,只是为了说明代字号格式可以在其他包中查找符号;如果你在CL-USER中进行事件处理,你可以忽略它):

(defpackage ex
  (:use "COMMON-LISP"))

(in-package #:ex)

(defun dot-cons (stream object &rest args)
  (declare (ignore args))
  (if (consp object)
      (format stream "(~/ex:dot-cons/ . ~/ex:dot-cons/)" (car object) (cdr object))
      (write object :stream stream)))

CL-USER> (format t "~/ex:dot-cons/" '(a b c))
(A . (B . (C . NIL)))
;=> NIL

答案 1 :(得分:3)

尝试了解它的作用:

CL-USER 9 > (format t "(~a ~a)" (princ 1) (princ 2))
12(1 2)
NIL

FORMAT是一个功能。首先评估参数。 (princ 1)得到评估。它打印1并返回1。然后评估(princ 2)。它打印2并返回2。使用已评估的参数调用函数FORMATt"(~a ~a)"12。它打印(1 2)并返回NIL

现在看看这个:

CL-USER 8 > (progn (princ "(")
                   (princ 1)
                   (princ " . ")
                   (princ 2)
                   (princ ")"))
(1 . 2)
")"

在打印cons单元时只需重复使用以上内容:

CL-USER 10 > (defun princme (c)
               (if (consp c)
                   (progn
                     (princ "(")
                     (princme (car c))
                     (princ " . ")
                     (princme (cdr c))
                     (princ ")"))
                 (princ c)))
PRINCME

CL-USER 11 > (princme '(1 2 3))
(1 . (2 . (3 . NIL)))

注意:生成字符串的原始递归show-dots不是一个好主意。为什么?因为它以递归方式收集字符串并可能产生大量垃圾......

CL-USER 14 > (trace show-dots)
(SHOW-DOTS)

CL-USER 15 > (show-dots '((1 2) (3 (4 (5 6) ))))
0 SHOW-DOTS > ...
  >> LST : ((1 2) (3 (4 (5 6))))
  1 SHOW-DOTS > ...
    >> LST : (1 2)
    2 SHOW-DOTS > ...
      >> LST : 1
    2 SHOW-DOTS < ...
      << VALUE-0 : "1"
    2 SHOW-DOTS > ...
      >> LST : (2)
      3 SHOW-DOTS > ...
        >> LST : 2
      3 SHOW-DOTS < ...
        << VALUE-0 : "2"
      3 SHOW-DOTS > ...
        >> LST : NIL
      3 SHOW-DOTS < ...
        << VALUE-0 : "NIL"
    2 SHOW-DOTS < ...
      << VALUE-0 : "(2 . NIL)"
  1 SHOW-DOTS < ...
    << VALUE-0 : "(1 . (2 . NIL))"
  1 SHOW-DOTS > ...
    >> LST : ((3 (4 (5 6))))
    2 SHOW-DOTS > ...
      >> LST : (3 (4 (5 6)))
      3 SHOW-DOTS > ...
        >> LST : 3
      3 SHOW-DOTS < ...
        << VALUE-0 : "3"
      3 SHOW-DOTS > ...
        >> LST : ((4 (5 6)))
        4 SHOW-DOTS > ...
          >> LST : (4 (5 6))
          5 SHOW-DOTS > ...
            >> LST : 4
          5 SHOW-DOTS < ...
            << VALUE-0 : "4"
          5 SHOW-DOTS > ...
            >> LST : ((5 6))
            6 SHOW-DOTS > ...
              >> LST : (5 6)
              7 SHOW-DOTS > ...
                >> LST : 5
              7 SHOW-DOTS < ...
                << VALUE-0 : "5"
              7 SHOW-DOTS > ...
                >> LST : (6)
                8 SHOW-DOTS > ...
                  >> LST : 6
                8 SHOW-DOTS < ...
                  << VALUE-0 : "6"
                8 SHOW-DOTS > ...
                  >> LST : NIL
                8 SHOW-DOTS < ...
                  << VALUE-0 : "NIL"
              7 SHOW-DOTS < ...
                << VALUE-0 : "(6 . NIL)"
            6 SHOW-DOTS < ...
              << VALUE-0 : "(5 . (6 . NIL))"
            6 SHOW-DOTS > ...
              >> LST : NIL
            6 SHOW-DOTS < ...
              << VALUE-0 : "NIL"
          5 SHOW-DOTS < ...
            << VALUE-0 : "((5 . (6 . NIL)) . NIL)"
        4 SHOW-DOTS < ...
          << VALUE-0 : "(4 . ((5 . (6 . NIL)) . NIL))"
        4 SHOW-DOTS > ...
          >> LST : NIL
        4 SHOW-DOTS < ...
          << VALUE-0 : "NIL"
      3 SHOW-DOTS < ...
        << VALUE-0 : "((4 . ((5 . (6 . NIL)) . NIL)) . NIL)"
    2 SHOW-DOTS < ...
      << VALUE-0 : "(3 . ((4 . ((5 . (6 . NIL)) . NIL)) . NIL))"
    2 SHOW-DOTS > ...
      >> LST : NIL
    2 SHOW-DOTS < ...
      << VALUE-0 : "NIL"
  1 SHOW-DOTS < ...
    << VALUE-0 : "((3 . ((4 . ((5 . (6 . NIL)) . NIL)) . NIL)) . NIL)"
0 SHOW-DOTS < ...
  << VALUE-0 : "((1 . (2 . NIL)) . ((3 . ((4 . ((5 . (6 . NIL)) . NIL)) . NIL)) . NIL))"
"((1 . (2 . NIL)) . ((3 . ((4 . ((5 . (6 . NIL)) . NIL)) . NIL)) . NIL))"

所有对FORMAT的调用都在分配新的字符串......

更好:

CL-USER 16 > (with-output-to-string (*standard-output*)
               (princme '((1 2) (3 (4 (5 6) )))))
"((1 . (2 . NIL)) . ((3 . ((4 . ((5 . (6 . NIL)) . NIL)) . NIL)) . NIL))"

一般认为在流输出方面,而不是字符串连接。

答案 2 :(得分:1)

如果返回字符串实际上是问题规范的一部分,那么使用CONCATENATE 'STRING。如果没有,请不要费心,只需坚持使用FORMAT T并打印到控制台。