Why Elm Does Not Have Statements

Image for post
Image for post

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

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

Let’s run it in Repl:

Image for post
Image for post

We used as the first attribute for Basketball.letter function 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 String.

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.

Written by

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