我读过很多关于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 lit)
.)
但无论如何,这里的问题是:
这(cond... (lit ...) ((or caps lit) ...))
在其中。我的理解是,这将转化为if(lit){ ... } else if(caps || lit){...}
C 风格的语法。那么 or 语句不是多余的吗?(or caps lit)
如果 caps 是,是否会调用该条件nil
?