从函数返回列表的元组

时间:2017-06-21 02:36:39

标签: elm

我想编写一个函数,在Elm中返回两个列表,但我遇到了问题。似乎编译器无法匹配空列表[]的类型。

import Html exposing (text)

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


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

编译器错误如下:

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 )

我还没有找到任何方法为空列表提供类型提示。检查文档,它没有那么深入: https://guide.elm-lang.org/core_language.html http://elm-lang.org/docs/syntax#functions

1 个答案:

答案 0 :(得分:3)

类型签名还需要(.., ..)元组语法,如:

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

[]是生成空列表的正确语法。如果您想了解有关List类型的更多信息,最好查看package.elm-lang.org上的文档。您分享的两个链接是更多"介绍指南"比综合文档。