..
Clojure 中的 sequential collection
Clojure Sequential Collections
Vectors
- are indexed 1
- are represented with []
- immutability
- add elements at the tail
-
indexed access
(get ["abc" false 3] 0)
-
count
(count [1 3 "abc"])
-
constructing
(vector 1 2 3)
-
add elements
(conj [1 2 3] 4 5 6)
Lists
- are sequential linked list
- add new elements at the head of the list
-
constructing
(def cards '(10 :ace :jack 9)) # 包含了4个元素,因为(会被认为是方法调用,所有要加 单引号
-
first
(first cards) (first (rest cards))
-
add
(conj cards :quene) # add front
-
stack access
# list can also be used as a stack (def stack '(:a :b)) (peek stack) # :a (pop stack) # :b