1
votes

This is related to the Elm Tutorials (http://guide.elm-lang.org/architecture/effects/random.html), and am trying to generate a list of random numbers (just 2 items for now) for one of the challenges.

I get a type error when trying to generate the list:

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)

This is the code I am using:

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)

I am still trying to get my head wrapped around types -- particularly with regard to lists. I'm guessing (List Int) means a list of integers, but I am not sure what List by itself means (list of arbitrary type?).

I have played around with the code by pulling out the Generator into a separate variable (intList) and also explicitly typing it. I also tried typing it Random.Generator List, which throws an error also. Basically, I could use help figuring out how to reconcile List vs. (List Int).

Thank you -- super new to Elm, so any guidance is appreciated.

1

1 Answers

2
votes

Based on the error message, it looks like you probably have defined NewFaces like this:

type Msg
  = Roll
  | NewFaces List

List takes a single type parameter, so it should be defined as

type Msg
  = Roll
  | NewFaces (List Int)