- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 301字
- 2021-08-05 10:42:45
Lists
Lists are created by wrapping the elements we want inside it with square brackets ([ and ]), separating the values with commas. Internally, lists are implemented as singly linked lists, meaning that accessing the elements of a list is a O(n) operation. Lists aren't stored contiguously in memory as arrays in other languages. As with tuples, list elements can be of any type:
iex> [1, :an_atom, 0.5]
[1, :an_atom, 0.5]
We have the ++ and -- operators that are exclusive to lists, and serve to concatenate and subtract lists, respectively:
iex> [0, 1, 1] ++ [2, 3, 5]
[0, 1, 1, 2, 3, 5]
iex> [0, 1, 1] -- [1, 2, 3]
[0, 1]
To check whether a certain element is present in a list, you can use the in operator:
iex> 1 in [0, 1, 1, 2, 3, 5]
true
iex> 99 in [0, 1, 1, 2, 3, 5]
false
To get the head of a list, we use the hd function, whereas to get the tail of a list, we use the tl function:
iex> hd([0, 1, 1, 2, 3, 5])
0
iex> tl([0, 1, 1, 2, 3, 5])
[1, 1, 2, 3, 5]
Notice that the semantic of tail here is the list without its head (which is also a list), and not the last element of a list. We'll be exploring this concept in more depth, along with some more examples on how to work with lists, in the Working with collections section. For reference, you can find a detailed list of operations you can make on lists at https://hexdocs.pm/elixir/List.html.
- Android Studio Essentials
- Vue.js前端開發基礎與項目實戰
- Java從入門到精通(第5版)
- 微服務設計原理與架構
- 智能手機APP UI設計與應用任務教程
- Flowable流程引擎實戰
- JavaScript腳本特效編程給力起飛
- Red Hat Enterprise Linux Troubleshooting Guide
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- Learning TypeScript
- MySQL數據庫應用技術及實戰
- Mathematica Data Visualization
- jQuery EasyUI從零開始學
- Learning IBM Bluemix
- C語言程序設計教程