Files
freeCodeCamp/guide/chinese/elixir/lists/index.md
2018-10-16 21:32:40 +05:30

42 lines
930 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Lists
localeTitle: 清单
---
## 清单
在Elixir中列表是由方括号内的值组成的数据结构。列表中的值可以是任何类型。
```elixir
iex> [1, "string", true]
[1, "string", true]
```
## 不变性
Elixir中的数据结构是不可变的因此在List上执行的任何操作都将返回一个新列表保留原始列表。
```elixir
iex> list = [1, "string", true]
[1, "string", true]
iex> list ++ [2]
[1, "string", true, 2]
iex> list
[1, "string", true]
```
## 头和尾
可以使用`hd/1``tl/1`运算符轻松访问列表的头部(第一个元素)和尾部(剩余值)。
```elixir
iex> list = [1, "string", true]
iex> hd(list)
1
iex> tl(list)
["string", true]
```
#### 更多信息:
* [elixir-lang.org |递归](https://elixir-lang.org/getting-started/basic-types.html#linked-lists)
* [hexdocs |枚举](https://hexdocs.pm/elixir/List.html)