Elm - 列表
列表、元組和記錄資料結構可以用來儲存值的集合。
本章討論如何在 Elm 中使用列表。
列表是同構值的集合。列表中的值必須都是相同的資料型別。
使用變數儲存值時,請考慮以下限制:
變數本質上是標量的。換句話說,在宣告時,一個變數只能儲存一個值。這意味著要在程式中儲存 n 個值,需要 n 個變數宣告。因此,當需要儲存大量值時,使用變數是不可行的。
程式中的變數以隨機順序分配記憶體,因此難以按宣告順序檢索/讀取值。
語法
List_name = [value1,value2,value3.....valuen]
示例
以下示例演示如何在 Elm 中使用列表。請在 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
列表操作
下表顯示了列表的常用操作:
| 序號 | 方法 | 描述 |
|---|---|---|
| 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) | 返回除第一個元素外的所有元素 |
isEmpty
此函式返回列表是否為空。
語法
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
此函式建立一個數字列表,每個元素遞增 1。將列表中應包含的最小和最大數字傳遞給函式。
語法
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