codecamp

Clojure 列表 List

List是用于存储数据项集合的结构。 在Clojure中,List实现了ISeq接口。 使用list函数在Clojure中创建列表。

以下是在Clojure中创建Numbers列表的示例:

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list 1 2 3 4)))
(example)

以上示例将输出以下结果:

(1 2 3 4)

以下在Clojure中创建字符型列表的示例:

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list 'a 'b 'c 'd)))
(example)

以上示例将输出以下结果:

(a b c d)

以下是Clojure中列表几个方法:

S.No.方法和说明
list* 创建一个新列表,其中包含其他项目,其中最后一个将被视为序列。
first此函数返回列表中的第一项。
nth此函数返回列表中“第n”位置的项目。
cons返回一个新列表,其中元素被添加到列表的开头。
conj返回一个新列表,其中列表在开头,要追加的元素放在末尾。
rest返回列表中第一个项目之后的剩余项目。

Clojure 字符串
Clojure 集合
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定