lisp - Lisp 示例冗余之地?

标签 lisp common-lisp clisp land-of-lisp

我读过很多关于 Land of Lisp 的好东西所以我想我可能会通过它看看有什么可看的。

(defun tweak-text (lst caps lit)
  (when lst
    (let ((item (car lst))
      (rest (cdr lst)))
      (cond 
       ; If item = space, then call recursively starting with ret
       ; Then, prepend the space on to the result.
       ((eq item #\space) (cons item (tweak-text rest caps lit)))
       ; if the item is an exclamation point.  Make sure that the
       ; next non-space is capitalized.
       ((member item '(#\! #\? #\.)) (cons item (tweak-text rest t lit)))
       ; if item = " then toggle whether we are in literal mode
       ((eq item #\") (tweak-text rest caps (not lit)))
       ; if literal mode, just add the item as is and continue
       (lit (cons item (tweak-text rest nil lit)))
       ; if either caps or literal mode = true capitalize it?
       ((or caps lit) (cons (char-upcase item) (tweak-text rest nil lit)))
       ; otherwise lower-case it.
       (t (cons (char-downcase item) (tweak-text rest nil nil)))))))

(评论是我的)
(仅供引用——方法签名是 (list-of-symbols bool-whether-to-caps bool-whether-to-treat-literally) 但是作者将它们缩短为 (lst caps点亮).)

但无论如何,问题是:
这里面有 (cond... (lit ...) ((or caps lit) ...))。我的理解是,这将转换为 C 风格语法中的 if(lit){ ... } else if(caps || lit){...}。那么 or 语句不是多余的吗?如果 caps 为 nil,是否会调用 (或 caps lit) 条件?

最佳答案

的确,你是对的。查看errata为了这本书。

Page 97: The function tweak-text has two glitches in it, though it will run OK on most Lisp implementations. First of all, it uses the eq function to compare characters- Characters should always be checked with other functions such as eql or char-equal as per the ANSI spec. Also, there's an unnecessary check of (or caps lit) that can be simplified to caps.

关于lisp - Lisp 示例冗余之地?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4576591/

相关文章:

lisp - 使用自定义函数 getter 作为 setter 和 setf

lisp - 这段代码的功能是什么?

json - 如何使用 yason 对 bool 值进行编码?

lisp - 如何使用 lisp (clisp) 创建和使用库?

lisp - % 运算符计算 CLISP 中的余数

lisp - 组织模式:org-agenda-files defvar 目录 "evaluation"?

function - 将 x 添加到列表的第 n 项的 lisp 函数

common-lisp - 相当于 virtualenv 或 rvm 的 Common Lisp?

web-services - 在长期运行的Common Lisp应用程序中,应使用什么策略来管理垃圾?

lisp - 在格式函数中使用列表的元素