Elm is a functional programming language. Its main influences are Haskell, OCaml, SML, and F#. Elm is strongly statically typed and it uses type inferencing. You don’t have to specify the type for variables and parameters like in C# or Java. Elm is built on the Haskell platform and has a Haskell-like syntax, though semantics are closer to OCaml.
I want to show you some basic features of Elm in this post. I’ll write some more posts about Elm in the next few weeks.
You will find an online editor on the Elm homepage where you can try out the programming language. Just click “Try” and you’ll get to the online editor. On the left side you’ll see the editor and on the right side the result when you run the program. You can run the program by clicking on “Compile” (below the editor).
We’re learning a new language and the first program in a new language is usually a “Hello, world” program. It looks like this in Elm:
main : Element main = plainText "Hello, world!"
main defines the entry point of the program. It is of type
Element which represents a rectangle on the screen with a known width and height. Elements are combined to define complex widgets and layouts. The type is defined in the library
Graphics.Element which is imported automatically when the program starts.
The first line of our program is the type signature. It defines the name and its type separated by a colon. The entry point has the name “main” and
Element is its type. The type signature is optional.
The second line is the code.
"Hello, world!" is a string. The function
plainText converts the string to an element. You can see, that you don’t need braces for calling a function.
Here is another version of the hello world program:
main = asText "Hello, world!"
Did you notice the difference? The second program prints the string “Hello, world!” with quotes and the first program without quotes. The first version uses the function
plainText to convert the string to an element.
plainText takes a string and creates the
Element from the string value.
The second program uses
asText which takes any value and uses its string representation for creating the
Element. The string representation of a string includes the quotes and the value does not.
main = asText (4,5)
This program prints the string representation of the tuple, “(4,5)”. A tuple is an ordered list of elements. The tuple above contains two integer values, 4 and 5. If you change
plainText you’ll get a type error.
plainText expects a string as parameter, but (4,5) is a tuple.
Elm is reactive
Elm has an interesting type called
Signal. A signal is a value which changes over time. Let’s see it in action:
import Mouse main : Signal Element main = lift asText Mouse.position
The first line imports a library for working with mouse input called
Mouse. The second line is the optional type signature. As you can see the type of the entry point
main has changed to
Signal Element. This means that output of the program can change over time.
Mouse module contains a signal called
position of type
Signal (Int,Int). Every time the mouse is moved its value changes. The value is of type
(Int,Int) which is a tuple with two integer numbers. It represents the current mouse position on the screen.
lift takes a function as first argument and a signal as second element. Every time the signal is updated the new value is applied to the function and its return value is used as new value for the signal returned by
lift. We use
asText as function so that
lift converts each tuple with the mouse positions to a string when the mouse position changes.
I showed you how a basic hello world program looks like in Elm and I introduced you to Elm’s concept of signals. I will focus on the functional nature of the language in my next post about Elm.