【问题标题】:How to return a list of elements in Elm如何在 Elm 中返回元素列表
【发布时间】:2016-04-20 00:20:34
【问题描述】:

我正在尝试从 ELM 中的简单数组构建元素列表。预期的结果实际上只是一个元素列表,其中 1 作为第一项,2 作为第二项,依此类推。

import Html exposing (..)
import Html.Attributes exposing (class, id)
import List exposing (map)

theArray = [1,2,3,4,5,6]

createListItem item =
  li [] [ text (toString item)]

buildList collection =
  map createListItem collection

builtList = ul [] [(buildList theArray)]

main = 
  builtList

但我在第 13 行不断收到编译器错误。我已经尝试将地图元素类型注释为 html,但我不知道我应该做什么。

The 2nd argument to function `ul` is causing a mismatch.

 *13| builtList = ul [] [(buildList theArray)]*

Function `ul` is expecting the 2nd argument to be:

    List VirtualDom.Node

But it is:

    List (List Html)

【问题讨论】:

    标签: elm


    【解决方案1】:

    buildList 已经返回了 List Html 类型的值,因此您不需要在 (buildList theArray) 周围加上括号。将第 13 行更改为:

    builtList = ul [] (buildList theArray)
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2016-04-02
      • 1970-01-01
      • 2021-02-07
      • 2021-09-08
      • 2019-10-31
      • 2016-11-09
      • 2021-07-07
      • 2023-03-07
      相关资源
      最近更新 更多