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)
条件的情况?
I've read a lot of good things about Land of Lisp so I thought that I might go through it to see what there was to see.
(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)))))))
(the comments are mine)
(FYI -- the method signature is (list-of-symbols bool-whether-to-caps bool-whether-to-treat-literally)
but the author shortened these to (lst caps lit)
.)
But anyway, here's the question:
This has (cond... (lit ...) ((or caps lit) ...))
in it. My understanding is that this would translate to if(lit){ ... } else if(caps || lit){...}
in a C style syntax. Isn't the or statement redundant then? Is there ever a condition where the (or caps lit)
condition will be called if caps is nil
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实,你是对的。请参阅本书的勘误表。
Indeed, you are correct. See the errata for the book.
我将其写为:
CASE 语句在角色上分派。然后 COND 语句处理其他条件。 CASE与EQL进行比较。这意味着 CASE 也适用于角色,甚至可以与多个项目进行比较。我也喜欢排列相应表达式的代码布局样式 - 这仅对等宽字体有用。这有助于我直观地检测代码中的模式,并有助于检测可以简化的代码。
DESTRCTURING-BIND 将列表分开。
为了有趣,使用 LOOP 重写:
I would write that as:
The CASE statement dispatches on the character. The COND statement then takes care of the other conditions. CASE compares with EQL. That means CASE works for also for characters and even can compare with multiple items. I'm also a fan of a code layout style that lines up corresponding expressions - this is only useful with monospaced fonts. This helps me to detect patterns visually in the code and helps detecting code that can be simplified.
DESTRUCTURING-BIND takes the list apart.
For fun, rewritten using LOOP: