Elm Language - Acquaint Yourself:

COSC4315 TT
5 min readFeb 25, 2019

--

Data Types, Expressions, & Abstractions

If you’ve not read part 1 please go here

Basics:

Elm is a Functional Reactive Programming (FRP) language for building web apps. Functional, in that it treats computation as the evaluation of mathematical functions and it avoids mutable (changeable) data. Reactive, in that it is affected by change. In other words, the variable a, in a = b+c, in an imperative language, maintains the same value regardless of the possible changes made to b or c later in the program. This is not the case with reactive programming. In reactive languages, the value of a is updated when the values of b or c change.

Data Types:

The primitives(simplest elements) in Elm are standard types: integer, float, string, and boolean.

Integers in Elm (Int) are whole numbers that can be represented in either normal base 10 or base 16 (hexadecimal) with mathematical operation being well-defined in the range: -2³¹:2³¹-1. When generating JavaScript code in the compiler the range safely expands to -2⁵³:2⁵³-1. Floating point numbers (float) follow the IEEE 754 standard utilized by the vast majority of hardware in existence. These can be defined using the standard form of 1.1112 or in scientific notation as 6.022e23. It is interesting to note that Elm has been designed to make all conversions between integers and floats explicit, rather than implementing an automatic conversion like JavaScript. For example, you cannot add an Int and a Float directly without first converting them to the same data type through the use of functions such as toFloat() or round. A third type is called ‘number’. This type will set itself to the correct data type, either int or float, based upon how it is used.

Text based data types include String and Char. In Elm you are required to use double quotes for Strings and single quotes for characters where a char must be a single character. For a multi line String you may utilize a set of three double quotes. This allows the compiler to view everything in between as a string containing newline characters without explicit escaping.

Boolean literals must either be True or False. Elm does not equate True and False to a number, like 1 and 0, but requires the evaluation of conditions to precisely match True or False.

To do this install Node.js. After, type elm repl.

Elm also has Collections which include Lists (require same type), Tuples(muti-type support), and Records(like dictionaries in Python) which hold key/value pairs.

Functions and Control Abstraction:

Being a declarative language, Elm implements control abstraction thoroughly. This means that it hides complicated code behind a simpler set of operations. In a declarative language, the focus is on what the computer is to do as opposed to how the computer should do it. For example, the core module, List, contains functions to work with lists. Creation, sorting, and ordering are handled for the programmer. Logically, the programmer can create them as well.

Elm does not natively support the traditional loop structure of other languages and relies on recursive functions to implement a similar structure. There does exist a package, elm-loop, that you can install to provide the more familiar while loop structure, but even this warns the programmer that they should be using mapping, folding, and recursion for the vast majority of operations.

Folds can be thought of as collapsing a structure into a value in a particular direction(left or right). Below is an example of how a for loop can be expressed as a recursive definition in Elm utilizing a left fold.

Mapping in Elm and JavaScript refers to applying a function to each item in the list using the List.map function. The code below shows an example of a map operation on a list of integers. It simply multiplies each item by 2 and returns a new list of number type.

This shows an example of a map operation being used to concatenate two lists into a new list having every possible combination of the two,(a1, a2, a3,…f5, f6)

For branching Elm uses case statements and traditional if/else assertions.

Expressions:

Elm supports many standard expression evaluations. Strings for example can be concatenated using the “++” operator. In addition, math utilizes the normal syntax and operators [+, -, *, /, //] that represent addition, subtraction, multiplication, floating point division, and integer division respectively.

Some unique expressions in Elm include: Let(values are defined in specific expression), Custom Types (new types), Type aliases(shorter name for another type), and function application operators.

let, custom type, and type alias

Function application operators allow the programmer to not have to use parentheses. It is a nice way to call functions.

Modules and Data Abstraction:

Modules are at the core for data abstraction in Elm. Data abstraction, allows the programmer to reduce cognitive load and risk of name conflicts. Data abstraction also hides data that is not explicitly imported. Modules allow programmers to work on the same project simultaneously.

importing modules

References:

https://guide.elm-lang.org/

https://elm-lang.org/docs

--

--