[英]How to create an empty collection in Clojure?
我正在嘗試使用 Clojure 對一組特定數據進行建模
一開始,我希望那段復雜的數據只是一個空集合。 在這一點上,我不在乎它是列表、集合還是向量。
(ns clojure-playground.core
(:gen-class))
(def my-complex-object (hash-set))
當我編譯這個文件並在 REPL 中輸入(my-complex-object)
時,我得到一個錯誤。
與
(def my-complex-object ())
,(def my-complex-object [])
,(def my-complex-object (list))
和(def my-complex-object '(list))
。 如何將my-complex-object
綁定到空集合?
您會收到錯誤,因為此表達式(my-complex-object)
調用名為 my-complex-object 的function 。 使用這些定義中的任何一個:
(def my-complex-object #{})
(def my-complex-object (hash-set))
(def my-complex-object ())
(def my-complex-object (list))
(def my-complex-object [])
(def my-complex-object (vector))
然后只評估my-complex-object
(不帶括號)。 如果您在 REPL 中同時執行這兩項操作,它將如下所示:
(def my-complex-object [])
=> #'clojure-playground.core/my-complex-object
my-complex-object
=> []
請注意,每種集合類型對於conj
、 pop
或處理重復項等操作都有不同的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.