Values in Elm

Elm has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

module Main exposing (..)

import Html exposing (Html, div, text)


main : Html msg
main =
    div []
        [ -- Strings, which can be added together with `++`.
          text ("elm" ++ "lang")
        , Html.br [] []

        -- Integers and floats.
        , text ("1+1 = " ++ String.fromInt (1 + 1))
        , Html.br [] []
        , text ("7.0/3.0 = " ++ String.fromFloat (7.0 / 3.0))
        , Html.br [] []

        -- Booleans, with boolean operators as you'd expect.
        , text (String.fromBool (True && False))
        , Html.br [] []
        , text (String.fromBool (True || False))
        , Html.br [] []
        , text (String.fromBool (not True))
        ]

To run this Elm program, you would typically compile it to JavaScript and then run it in a browser. Here’s what the output would look like:

elmlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
False
True
False

In Elm, we don’t have a direct equivalent of fmt.Println. Instead, we create an HTML structure and use the text function to display strings. We also use String.fromInt, String.fromFloat, and String.fromBool to convert other types to strings for display.

Note that in Elm, string concatenation is done with ++ instead of +. Also, Elm is a purely functional language, so we define a main function that returns an HTML structure, rather than having a series of statements that print to the console.