Search code examples
elmlet

Elm: Partial Function Application and Let


The Beginning Elm - Let Expression page builds on the previous page, but it doesn't cover how to update the main function, written in forward function notation, which was:

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

to include the new fuelStatus parameter.

The compiler complains about a type mismatch, which is correct, as escapeEarth now has a third argument, which is a string.

As stated on that site "The forward function application operator takes the result from the previous expression and passes it as the last argument to the next function application."

In other words, how do I write this:

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

using forward notation?

Also, why doesn't this print "Land on droneship", along with "Stay in orbit"? It only prints "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")

Solution

  • I think what you need is

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

    In other words you define a little anonymous function to insert the value correctly. You may want to look at whether the escapeEarth function should be defined with a different order.

    An alternative if you love 'point free' would be

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

    Some would argue that this is less clear though

    As for your second question you have defined functions in your let statement but never actually used it