In Elm functions are a bit different than in languages like JavaScript, so it is worth familiarising yourself with how they are written and called in Elm.
Let’s create a function in the Elm repl (read-evaluate-print loop). In your terminal, start a repl session:
elm-repl
The first time you use the repl Elm needs to download the core modules, so it can take a little while. Try:
1 + 1
It should print 2 : number
after a few seconds. From that point on, responses should be immediate.
Create a function add
:
add a b = a + b
This is a function called add
that takes two arguments a
and b
. In Elm you use spaces to separate the arguments, not parentheses or commas. In JavaScript this is equivalent to:
// ES5
function add(a, b) {
return a + b
}
// ES6 arrow function
const add = (a, b) => a + b
You can call this function like so:
add 1 2
Note how we use spaces to pass arguments to the function.
Partial application is very common in Elm so is important to explain early.
If you pass only one argument to this function you will get back a partially applied function:
add2 = add 2
Then you can pass the missing argument to get the final result:
add2 3 -- 5 : number
join "Hello" "World"
Should return Hello World
. The string concatenation operator in Elm is ++
.
sayHello "Sam"
Should return Hello Sam
.
:exit
to exit the repl.