【发布时间】:2011-01-02 00:58:00
【问题描述】:
我已经很多阅读了关于 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)))))))
(cmets 是我的)
(仅供参考——方法签名是(list-of-symbols bool-whether-to-caps bool-whether-to-treat-literally),但作者将其缩短为(lst caps lit)。)
但无论如何,这里的问题是:
这里面有(cond... (lit ...) ((or caps lit) ...))。我的理解是,这将以 C 样式语法转换为 if(lit){ ... } else if(caps || lit){...}。那么 or 语句不是多余的吗?如果 caps 为 nil,是否会调用 (or caps lit) 条件?
【问题讨论】:
标签: lisp common-lisp clisp land-of-lisp