1
votes

I'd like to write a function that returns two lists in Elm but I'm running into issues. It seems like the compiler cannot match the types of the empty list [].

import Html exposing (text)

main =
  let
    (a, b) = genList
  in
    text "Hello"


genList: List Float List Float
genList =
  ([], [])

The compiler errors are as follows:

Detected errors in 1 module.


-- TYPE MISMATCH ---------------------------------------------------------------

`genList` is being used in an unexpected way.

6|     (a, b) = genList
                ^^^^^^^
Based on its definition, `genList` has this type:

    List Float List Float

But you are trying to use it as:

    ( a, b )


-- TYPE MISMATCH ---------------------------------------------------------------

The definition of `genList` does not match its type annotation.

11| genList: List Float List Float
12| genList =
13|   ([], [])

The type annotation for `genList` says it is a:

    List Float List Float

But the definition (shown above) is a:

    ( List a, List b )

I haven't found any way of giving a type hint for the empty list. Checking the documentation, it doesn't go that deep: https://guide.elm-lang.org/core_language.html http://elm-lang.org/docs/syntax#functions

1

1 Answers

3
votes

The type signature also needs the (.., ..) tuple syntax like:

genList: (List Float, List Float)
genList =
  ([], [])

[] is the correct syntax for generating an empty list though. If you want to know more about the List type, it's probably better to look at the docs on package.elm-lang.org. The two links you shared are more "intro guides" than comprehensive docs.