2012-01-26 48 views
4
------------------------- 
clojure.core/seq 
([coll]) 
    Returns a seq on the collection. If the collection is 
    empty, returns nil. (seq nil) returns nil. seq also works on 
    Strings, native Java arrays (of reference types) and any objects 
    that implement Iterable. 
------------------------- 
clojure.core/seq? 
([x]) 
    Return true if x implements ISeq 
----- 

顯然是空的?基於seq。空有什麼區別?而且無?我很困惑。seq和seq有什麼區別?

clojure.core/empty? 
([coll]) 
    Returns true if coll has no items - same as (not (seq coll)). 
    Please use the idiom (seq x) rather than (not (empty? x)) 

多:

(not (seq?())) ;;false 
(not (seq())) ;;true 
(not nil) ;;true 

回答

10
  • seq轉換集合序列,並返回nil,如果集合是空的;如果參數爲零,也返回nil。
  • seq?如果參數是一個序列(實現ISeq接口),則返回true。
  • empty?將返回true如果參數是零或一個空集合。
  • nil?如果參數爲零,則返回true。

我猜對(seq x)成語中的文檔字符串爲empty?位適用於常見的做法使用if-let像這樣的:

(defn print-odd-numbers [coll] 
    (if-let [x (seq (filter odd? coll))] 
    (println "Odd numbers:" x) 
    (println "No odd numbers found.")))