gpt4 book ai didi

lisp - Lisp 示例冗余之地?

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:08 24 4
gpt4 key购买 nike

我读过很多关于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com