是否有类似于 Elm 中的列表推导式的东西?

问题描述

如果我理解正确,Elm 没有列表理解之类的东西。

例如,如果您想将数字 1 到 100 映射到其他内容,您会使用什么代替?

解决方法

我认为 List.range 和管道风格一起读得很好。 但是不如python中的list comprehension那么简洁。

module Main exposing (main)

import Html


main =
    List.range 1 10
        |> List.map square
        |> List.map String.fromInt
        |> String.join ","
        |> Html.text


square : Int -> Int
square a =
    a ^ 2