freeCodeCamp/guide/chinese/elixir/lists/index.md

930 B
Raw Blame History

title localeTitle
Lists 清单

清单

在Elixir中列表是由方括号内的值组成的数据结构。列表中的值可以是任何类型。

iex> [1, "string", true] 
 [1, "string", true] 

不变性

Elixir中的数据结构是不可变的因此在List上执行的任何操作都将返回一个新列表保留原始列表。

iex> list = [1, "string", true] 
 [1, "string", true] 
 iex> list ++ [2] 
 [1, "string", true, 2] 
 iex> list 
 [1, "string", true] 

头和尾

可以使用hd/1tl/1运算符轻松访问列表的头部(第一个元素)和尾部(剩余值)。

iex> list = [1, "string", true] 
 iex> hd(list) 
 1 
 iex> tl(list) 
 ["string", true] 

更多信息: