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 lit)
。)
但无论如何,这是一个问题:
这里有(cond... (lit ...) ((or caps lit) ...))
。我的理解是,这将在C风格语法中转换为if(lit){ ... } else if(caps || lit){...}
。这个或陈述不是多余的吗?如果上限是nil
,是否有条件会调用(or caps lit)
条件?
没有找到相关结果
已邀请:
2 个回复
贸会
辅奈
CASE语句将调度该角色。然后COND声明处理其他条件。 CASE与EQL进行比较。这意味着CASE也适用于角色,甚至可以与多个项目进行比较。我也是代码布局样式的粉丝,它排列了相应的表达式 - 这仅适用于等宽字体。这有助于我在代码中直观地检测模式,并帮助检测可以简化的代码。 DESTRUCTURING-BIND将列表分开。 为了好玩,使用LOOP重写: