在Elm中生成随机整数列表时发生类型冲突
这与Elm教程()有关,我正在尝试为其中一个挑战生成一个随机数列表(目前只有2项) 尝试生成列表时出现类型错误:在Elm中生成随机整数列表时发生类型冲突,elm,Elm,这与Elm教程()有关,我正在尝试为其中一个挑战生成一个随机数列表(目前只有2项) 尝试生成列表时出现类型错误: The 2nd argument to function `generate` is causing a mismatch. 39| Random.generate NewFaces intList) ^^^^^^^ Function `generate` is expecti
The 2nd argument to function `generate` is causing a mismatch.
39| Random.generate NewFaces intList)
^^^^^^^
Function `generate` is expecting the 2nd argument to be:
Random.Generator List
But it is:
Random.Generator (List Int)
这是我正在使用的代码:
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
let
intList : Random.Generator (List Int)
intList =
Random.list 2 (Random.int 1 6)
in
(model, Random.generate NewFaces intList)
NewFaces newFaces ->
({ model | dieFaces = newFaces}, Cmd.none)
我仍在努力让我的头脑专注于类型——特别是关于列表。我猜(List Int)意味着整数列表,但我不确定列表本身意味着什么(任意类型的列表?)
我将生成器拉到一个单独的变量(intList)中,并显式地键入它,以此来处理代码。我还试着输入Random.Generator List,它也会抛出一个错误。基本上,我可以使用帮助找出如何协调列表与(列表Int)
谢谢您——对于Elm来说,这是一个全新的概念,因此我们非常感谢您提供的任何指导。根据错误消息,您可能已经将
新面孔定义为以下内容:
type Msg
=滚动
|新面孔列表
List
采用单个类型参数,因此应将其定义为
type Msg
=滚动
|新面(列表整型)
哦!好的,所以它期望列表的原因是因为我说NewFaces应该期望列表而不是(List Int)-这很有道理,谢谢!!我非常专注于Random.generate函数,甚至没有考虑NewFaces消息。