930 B
930 B
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/1
和tl/1
运算符轻松访问列表的头部(第一个元素)和尾部(剩余值)。
iex> list = [1, "string", true]
iex> hd(list)
1
iex> tl(list)
["string", true]