Elm语言创建列表
示例
> listOfNumbers = [1,4,99] [1,4,99] : List number > > listOfStrings = ["Hello","World"] ["Hello","World"] : List String > > emptyList = [] -- can be anything, we don't know yet [] : List a >
在底层,List(链接列表)由::函数(称为“cons”)构造,该函数带有两个参数:一个元素(称为头)和一个(可能为空)列表(头在其前面)。
> withoutSyntaxSugar = 1 :: [] [1] : List number > > longerOne = 1 :: 2 :: 3 :: [] [1,2,3] : List number > > nonemptyTail = 1 :: [2] [1,2] : List number >
List只能采用一种类型的值,因此[1,"abc"]不可能实现。如果需要,请使用元组。
> notAllowed = [1,"abc"]
==================================== ERRORS ====================================
-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm
The 1st and 2nd elements are different types of values.
8| [1,"abc"]
^^^^^
The 1st element has this type:
number
But the 2nd is:
String
Hint: All elements should be the same type of value so that we can iterate
through the list without running into unexpected values.
>
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练