Why Elm Does Not Have Statements

TL;DR

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.[1] 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 module, we add function that is using as argument a function. We call a metafunction. The code is in the above image.

We exposed function with three arguments, , and . In the function body, and are used as functions with one argument .

Let’s run it in :

We used as the first attribute for function function, and the second attribute is function. And this is Elm magic, the function input parameter could be another function.

Second use of 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 function. So we must use to avoid a syntax error. The requirement is that anonymous function must have the signature as declared in function, the input is , and it must return .

Here is what happens when we declare an anonymous function with Int as return value:

Remember

  • 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.

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store