Autoplay

    Explore the Elm Type System with the Elm Repl

    Enrico BuonannoEnrico Buonanno

    In this lesson we'll use the Elm REPL to learn about the various types in Elm, such as String, Bool, Float, and Int. We'll also see how all operators in Elm are just functions.

    Try it out for yourself here in the Elm repl!

    elmElm
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 A good way to start learning about types is by using the Elm repo. In your terminal, just type Elm repo, and now you can start entering values into the repo. If you enter "hello" with quotes, it will tell you that the string hello is a value of type STRING, or if you enter true, it tells you that true is a BOOL.

    00:21 Notice that types are uppercase. As far as numbers, there are two number types in Elm to represent integers and floating point numbers. If you enter 2.5, that's a float, and if you enter 2, the repo tells us that that's of type number.

    00:38 Notice that number is lowercase, so it's a bit different than the types we've seen so far. Number is a special type that can be used as a float or as an int, depending on context. Let's look at some of the built-in functions for working with numbers.

    00:52 For example, square root. The repo tells you that that's a function that takes a float and returns a float. If you write square root of two, that value two is going to be interpreted as a float now. On the other hand, the round function takes a float and returns and int.

    01:09 If you take the square root of two, which is naturally a float, and then pass that to round, what you get back is an int. What about operators for working with numbers? When you say one plus two, for example, we get naturally a three, and that's a number. Again, it can be used as an int or as a float, but what happens, exactly?

    01:31 Operators in Elm are just functions. If you want to refer to them as functions, you just put them between parentheses like this. Indeed, the repo tells us that this is a function that takes two numbers and returns a number.

    01:45 When you say one plus two, these are indeed two numbers, and we get a number, the value three. If you were to say 1 plus 2.5, then it recognizes that the second number is actually a float, so the result is a float as well.

    02:01 The same holds for other operators, for example the Boolean operators. If you say true and false, that's false naturally, and you can refer to the and function like this. This is of course a function that takes two Booleans and returns a Boolean.

    Discuss

    Discuss