我已经阅读了很多关于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) ...))
。我的理解是,在C风格的语法中,这将被翻译为if(lit){ ... } else if(caps || lit){...}
。那么or语句是否多余呢?如果caps是nil
,是否会出现调用(or caps lit)
条件的情况?