请注意 rest和 next的行为差异。结合 seq,这导致了以下习语,其中列表结束通过 seq测试,列表的其余部分通过 rest获得(改编自“ The Joy of Clojure”) :
; "when (seq s)":
; case s nonempty -> truthy -> go
; case s empty -> nil -> falsy -> skip
; case s nil -> nil -> falsy -> skip
(defn print-seq [s]
(when (seq s)
(assert (and (not (nil? s)) (empty? s)))
(prn (first s)) ; would give nil on empty seq
(recur (rest s)))) ; would give an empty sequence on empty seq