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上的常见操作
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
此将列表中的值从最低到最高排序。
语法
Listrt list_name
要检查的签名,请在elm REPL中键入以下
> Listrt <function> : List comparable -> List comparable
示例
> Listrt [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