Convert Bool to String

Viewed 1777

I am trying to print a boolean result onto my screen using Elm 19.

result =
  add 1 2 |> (\a -> modBy 2 a == 0)

main =
  Html.text (String.fromBool result)

I know fromInt works when the equation spit out an Int but I don't know how to turn the bool into a String for my current problem.

1 Answers

Converting a Bool to a String is no different than doing anything else with it. It has just two values, True and False, and it even has its own construct to branch on it, the if expression. It's therefore as simple as:

stringFromBool : Bool -> String
stringFromBool value =
  if value then
    "True"

  else
    "False"

then replace String.fromBool in your code with this function:

main =
  Html.text (stringFromBool result)
Related