Elm:部分函数应用和 Let

Elm: Partial Function Application and Let

Beginning Elm - Let Expression 页面建立在上一页的基础上,但它不包括如何更新主函数,用正向函数表示法编写,即:

main =
    time 2 3
        |> speed 7.67
        |> escapeEarth 11
        |> Html.text

包括新的 fuelStatus 参数。

编译器抱怨类型不匹配,这是正确的,因为 escapeEarth 现在有第三个参数,它是一个字符串。

如该网站所述"The forward function application operator takes the result from the previous expression and passes it as the last argument to the next function application."

换句话说,我该怎么写:

Html.text (escapeEarth 11 (speed 7.67 (time 2 3)) "low")

使用正向表示法?

此外,为什么不打印 "Land on droneship" 和 "Stay in orbit"?它只打印 "Stay in orbit":

module Playground exposing (..)

import Html


escapeEarth velocity speed fuelStatus =
    let
        escapeVelocityInKmPerSec =
            11.186

        orbitalSpeedInKmPerSec =
            7.67

        whereToLand fuelStatus =
            if fuelStatus == "low" then
                "Land on droneship"
            else
                "Land on launchpad"
    in
    if velocity > escapeVelocityInKmPerSec then
        "Godspeed"
    else if speed == orbitalSpeedInKmPerSec then
        "Stay in orbit"
    else
        "Come back"


speed distance time =
    distance / time


time startTime endTime =
    endTime - startTime


main =
    Html.text (escapeEarth 11 (speed 7.67 (time 2 3)) "low")

我觉得你需要的是

main =
    time 2 3
        |> speed 7.67
        |> \spd -> escapeEarth 11 spd "low"
        |> Html.text

换句话说,您定义了一个小的匿名函数来正确插入值。您可能想看看是否应该以不同的顺序定义 escapeEarth 函数。

如果你喜欢'point free',另一种选择是

main =
    time 2 3
        |> speed 7.67
        |> flip (escapeEarth 11) "low"
        |> Html.text

有些人会争辩说这不太清楚

关于你的第二个问题,你在 let 语句中定义了函数,但从未真正使用过它