Here we explain Elm creator design decision to not support programming statements. This post is part of the functional language series, and it is based on a remarkable book Programming Elm Build Safe and Maintainable Front-End Applications by Jeremy Fairbank.
What is a programming statement:
In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g., expressions) [wikipedia].
Elm calls for a different mindset. Elm functions are building blocks for other functions. The result is less Elm code. In our
Basketball module, we add
letter function that is using as argument a function. We call
letter a metafunction. The code is in the above image.
letter function with three arguments,
height. In the function body,
second are used as functions with one argument
Let’s run it in
We used as the first attribute for
Basketball.trials function, and the second attribute is
Basketball.position function. And this is Elm magic, the function input parameter could be another function.
Second use of
Basketball.letter function is using an anonymous function. An anonymous function is a function without a name, and you need to remember the syntax for an anonymous function declaration. We declared anonymous function in a call to
Basketball.letter function. So we must use
() to avoid a syntax error. The requirement is that anonymous function must have the signature as declared in
Basketball.letter function, the input is
Int, and it must return
Here is what happens when we declare an anonymous function with Int as return value:
- Elm does not support statements
- the function accepts as input another function
- how to write an anonymous function
Originally published at https://blog.tentamen.eu on June 18, 2020.