Subscribe now

Introduction and Installing [05.07.2016]

Elm is a strongly-typed functional programming language for the browser. It's one of the most fun experiences I've had with a new language in a few years. To begin exploring it, we'll walk through the installation process. Let's get started.

Installing

There are a couple of straightforward installers for Mac and Windows available at the elm install page, but you can also install elm via npm if you have that installed. I'll walk through the npm installation process because the native installers are pretty self explanatory, and because I tend to use linux.1

It's super simple. You just use npm to globally install the elm package:

sudo npm install -g elm

That's really it. With any of the installers, you can just run them again to upgrade to the latest version. To make sure it's installed, you can run elm-repl to get into the language's repl.

Intro to the REPL

elm-repl
# Note: alternatively, you can run this as `elm repl` - and that's true of all
# elm subcommands.

This is a basic REPL. Let's explore some basic things in the language now.

Numbers

Numbers are a basic type in every language.

> 1
1 : number

Here we can see that the repl returns its value and its type for you to see. Let's look at basic operations on numbers:

You can add:

> 1 + 1
2 : number

You can subtract:

> 3 - 2
1 : number
> (1 + 2) - 2
1 : number

You can multiply:

> 3 * 2
6 : number

And you can divide:

> 4 / 2
2 : Float
> 5 / 2
2.5 : Float

The normal division symbol performs floatwise division. If you want an Integer returned, you can use the double-slash:

> 4 // 2
2 : Int
> 5 // 2
2 : Int

There are obviously a lot more things you can do to numbers, but we'll get to that later.

Chars

Single letters are of type Char, and you surround them with single quotes.

> 'a'
'a' : Char

It's invalid to try to make a multi-character Char:

> 'ab'
-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm

Elm uses double quotes for strings. Switch the ' to " on both ends of the string
and you should be all set!

3|   'ab'
        ^
Maybe <http://elm-lang.org/docs/syntax> can help you figure it out.

This is our first time seeing Elm's error messages. You'll end up loving these because they're so unbelievably descriptive, due to the strong type system plus meticulous attention to detail.

Strings

If you want a string, you use double quotes:

> "Hello"
"Hello" : String

You can concatenate strings easily:

> "Hello" ++ "World"
"HelloWorld" : String

You can also build multi-line strings with 3 double quotes on either side. In the REPL, this is a little odd as you have to end any multi-line expressions in the REPL with a backslash at the end of each line:

> """\
| foo\
| bar\
| """
"\n  foo\n  bar\n  " : String

Booleans

Booleans are just True and False:

> True
True : Bool
> False
False : Bool

Boolean operations are as you'd expect:

> False || True
True : Bool
> True && False
False : Bool

Lists

If you want a list of things, that's easy enough. Lists must hold values of the same type:

> [1, 2, 3]
[1,2,3] : List number

Here you can see that this is a list of numbers. What happens if we try to put different types into a list?

> [1, "Foo"]
-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm

The 1st and 2nd entries in this list are different types of values.

3|   [1, "Foo"]
         ^^^^^
The 1st entry has this type:

    number

But the 2nd is:

    String

Hint: Every entry in a list needs to be the same type of value. This way you
never run into unexpected values partway through. To mix different types in a
single list, create a "union type" as described in:
<http://guide.elm-lang.org/types/union_types.html>

There we see Elm's fantastic error messages once again, explaining exactly what the problem is, and in this case easing you into a slightly more advanced topic: Union Types.

Tuples

Tuples hold a fixed number of values, and the values can be of any type:

> (1, "Foo")
(1,"Foo") : ( number, String )

Records

Records are a set of key/value pairs. Think of them like a hash in ruby, or an object in JavaScript:

> point = { x = 3, y = 4 }
{ x = 3, y = 4 } : { x : number, y : number1 }

The additional '1' at the end of the number type here is just a kind of quirk that I'd rather not get into in detail yet.2

You can access their values by name:

> point.x
3 : number

You can also access them by name with something akin to prefix notation. You basically are calling a function on the record. This becomes useful later, but for now I'll just show you:

> .x point
3 : number

You can update a record, but values in Elm are immutable so you will just get a new one without modifying the existing record:

> { point | x = 6 }
{ x = 6, y = 4 } : { x : number, y : number1 }
> point
{ x = 3, y = 4 } : { x : number, y : number1 }

Summary

And that's it. This was a quick summary of the language basics and an explanation of installation. I hope you enjoyed it. See you soon!

Resources


  1. If you want a version manager, I prefer asdf, which has an elm plugin, which I've sent a Pull Request to that adds support for Elm 0.18. 

  2. The point is that it's a number type, but it might not be the same number type as the other one, once type unification happens.