Elm 列表

Elm 列表

列表,元组和记录数据结构可用于存储值的集合。

本章讨论如何在Elm中使用List。

列表是齐次值的集合。列表中的值必须全部具有相同的数据类型。

使用变量存储值时,请考虑以下限制-

  • 变量本质上是标量。换句话说,在声明时,变量只能保存一个值。这意味着要在程序中存储n个值,将需要n个变量声明。因此,当需要存储更大的值集合时,使用变量是不可行的。

  • 程序中的变量以随机顺序分配给内存,因此很难按声明的顺序检索/读取值。

语法

List_name = [value1,value2,value3.....valuen]

示例

以下示例显示了如何在Elm中使用列表。在榆树REPL中尝试这个例子

> myList1 = [10,20,30]
[10,20,30] : List number
> myList2 = ["hello","world"]
["hello","world"] : List String

如果我们尝试将不同类型的值添加到列表中,则编译器将引发类型不匹配错误。

如下所示。

> myList = [1,"hello"]
-- TYPE MISMATCH 
--------------------------------------------- 
repl-temp-000.elm

The 1st and 2nd entries in this list are different types of values.

4| [1,"hello"]
^^^^^^^
The 1st entry has this type:
   number
But the 2nd is:
   String

List 常见方法

下表显示了List上的常见操作

序号 方法 描述
1 isEmpty : List a -> Bool 检查列表是否为空
2 reverse : List a -> Bool 反转输入列表
3 length : List a -> Int 返回列表的大小
4 maximum : List comparable -> Maybe.Maybe comparable 返回最大值
5 minimum : List comparable -> Maybe.Maybe comparable 返回最小值
6 sum : List number -> number 返回列表中所有元素的总和
7 product : List number -> number 检查列表是否为空
8 sort : List comparable -> List comparable 以升序对列表进行排序
9 concat : List (List a) -> List a 将一堆列表合并为一个
10 append : List a -> List a -> List a 合并两个列表
11 range : Int -> Int -> List Int 从头到尾返回数字列表
12 filter : (a -> Bool) -> List a -> List a 从输入列表中过滤值列表
13 head : List a -> Maybe.Maybe a 返回列表中的第一个元素
14 tail : : List a -> Maybe.Maybe (List a) 返回除head以外的所有元素

isEmpty

如果列表为空,则此函数返回true。

语法

List.isEmpty list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.isEmpty
<function> : List a -> Bool

示例

> List.isEmpty
<function> : List a -> Bool

> List.isEmpty [10,20,30]
False : Bool

reverse

此功能将反转列表。

语法

List.reverse list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.reverse
<function> : List a -> List a

示例

> List.reverse [10,20,30]
[30,20,10] : List number

length

此函数返回列表的长度。

语法

List.length list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.length
<function> : List a -> Int

示例

> List.length [10,20,30]
3 : Int

maximum

此函数返回非空列表中的最大元素。

语法

List.maximum list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.maximum
<function> : List comparable -> Maybe.Maybe comparable

示例

> List.maximum [10,20,30]
Just 30 : Maybe.Maybe number
> List.maximum []
Nothing : Maybe.Maybe comparable

minimum

此函数返回非空列表中的最小元素。

语法

List.minimum list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.minimum
<function> : List comparable -> Maybe.Maybe comparable

示例

> List.minimum [10,20,30]
Just 10 : Maybe.Maybe number

sum

此函数返回列表中所有元素的总和。

语法

List.sum list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.sum
<function> : List number -> number

示例

> List.sum [10,20,30]
60 : number

product

此函数返回列表中所有元素的乘积。

语法

List.product list_name

要检查功能的签名,请在elm REPL中键入以下内容

<function>  : List number ->  number

示例

List.product [10,20,30]
6000 : number

sort

此函数将列表中的值从最低到最高排序。

语法

List.sort list_name

要检查功能的签名,请在elm REPL中键入以下内容

> List.sort
<function> : List comparable -> List comparable

示例

> List.sort [10,20,30]
[10,20,30] : List number

concat

此函数将一堆列表连接成一个列表。

语法

List.concat [ [list_name1],[list_name2],[list_name3],.....[list_nameN] ]

要检查功能的签名,请在elm REPL中键入以下内容

> List.concat
<function> : List (List a) -> List a

示例

> List.concat [[10,20], [30,40],[50,60]]
[10,20,30,40,50,60] : List number

append

此功能将两个列表放在一起。

语法

List.append [list_name1] [list_name2]

要检查功能的签名,请在elm REPL中键入以下内容

> List.append
<function> : List a -> List a -> List a

示例

> List.append [10,20] [30,40]
[10,20,30,40] : List number

++运算符还可用于将列表追加到另一个列表。这在下面的示例中显示

> [10.1,20.2] ++ [30.3,40.4]
[10.1,20.2,30.3,40.4] : List Float

range

此函数创建一个数字列表,每个元素加一。列表中的最低和最高数字将传递给该函数。

语法

List.range start_range end_range

要检查功能的签名,请在elm REPL中键入以下内容

> List.range
<function> : Int -> Int -> List Int

示例

> List.range 1 10
[1,2,3,4,5,6,7,8,9,10] : List Int

filter

此功能从输入列表中过滤出一组值。仅保留通过测试的值。

语法

List.filter test_function input_list

要检查功能的签名,请在elm REPL中键入以下内容

> List.filter
<function> : (a -> Bool) -> List a -> List a

示例

以下示例过滤输入列表中的所有偶数

> List.filter (\n -> n%2==0) [10,20,30,55]
[10,20,30] : List Int

head

此函数返回输入列表中的第一个元素。

语法

List.head input_list

要检查功能的签名,请在elm REPL中键入以下内容

> List.head
<function> : List a -> Maybe.Maybe a

示例

> List.head [10,20,30,40]
Just 10 : Maybe.Maybe number
> List.head []
Nothing : Maybe.Maybe a

tail

此函数返回列表中第一位之后的所有元素。

语法

List.tail input_list

要检查功能的签名,请在elm REPL中键入以下内容

> List.tail
<function> : List a -> Maybe.Maybe (List a)

示例

> List.tail [10,20,30,40,50]
Just [20,30,40,50] : Maybe.Maybe (List number)
> List.tail [10]
Just [] : Maybe.Maybe (List number)
> List.tail []
Nothing : Maybe.Maybe (List a)

使用Cons运算符

cons运算符(::)将元素添加到列表的前面。

示例

> 10::[20,30,40,50]
[10,20,30,40,50] : List number

要添加的新元素和列表中值的数据类型必须匹配。如果数据类型不匹配,则编译器将引发错误。

> [1,2,3,4]::[5,6,7,8]
-- TYPE MISMATCH ---------------------------------
------------ repl-temp-000.elm

The right side of (::) is causing a type mismatch.

3| [1,2,3,4]::[5,6,7,8]
			  ^^^^^^^^^
(::) is expecting the right side to be a:

   List (List number)

But the right side is:

   List number
Hint: With operators like (::) I always check the left side first. If it seems fine, 
I assume it is correct and check the right side. So the 
problem may be in how the left and right arguments interact.

列表是不可变的

让我们检查清单在Elm中是否是不变的。第一个列表myList与值1串联时,将创建一个新列表,并返回到myListCopy。因此,如果我们显示初始列表,则其值将不会更改。

> myList = [10,20,30]
[10,20,30] : List number
> myListCopy = 1::myList
[1,10,20,30] : List number
> myList
[10,20,30] : List number
>myList == myListCopy
False : Bool