Elm (programming language)

This is an old revision of this page, as edited by 2620:0:1000:157d:3827:5b11:c0ee:33dd (talk) at 19:25, 15 November 2012 (Syntax and Semantics). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Elm is a functional programming language for declaratively creating graphical user interfaces. Elm uses the Functional Reactive Programming style and purely functional graphical layout to build user interface without any destructive updates.

Elm
Paradigmfunctional reactive
Designed byEvan Czaplicki
First appeared2011
Stable release2.5.8 (August 18, 2005; 20 years ago (2005-08-18)) [±]
Filename extensions.elm
Websiteelm-lang.org

Description

The primary implementation of Elm compiles to HTML, CSS, and JavaScript. Functional Reactive Programming takes the place of event handlers and callbacks; it also manages all screen updates automatically. Purely functional graphical layout takes the place of working with the DOM.

Elm allows Markdown to be embedded directly, so users can create text content in a familiar and natural way.

Syntax and Semantics

Elm's version of Functional Reactive Programming is event-driven, meaning that updates are only performed as necessary. It shares the closest resemblance to Event-Driven FRP and Arrowized FRP.

The following program displays the position of the mouse as it moves around the screen, automatically updating the screen in real-time.

main = lift asText Mouse.position

The value of main is displayed on screen. Function asText turns any value into a displayable textual representation. Mouse.position is a value that changes over time, called a signal. Function lift ensures that asText is applied to Mouse.position every time the mouse moves.

Elm's list interpolation allows sequences of numbers to be easily created.

factorial n = product [1..n]

Elm has a small but expressive set of language constructs, including if-expressions, let-expressions, case-expressions, and anonymous functions.

-- if-expression
makePositive n = if n < 0 then 0-n else n

-- let-expressions
keepEvens1 numbers =
    let isEven n = n `mod` 2 == 0 in
    filter isEven numbers

-- case-expression
keepEvens2 numbers =
    case numbers of
    { hd : tl -> if hd `mod` 2 == 0 then hd : keepEvens2 tl
                                    else keepEvens2 tl
    ; [] -> [] }

-- anonymous functions
keepEvens3 numbers = filter (\n -> n `mod` 2 == 0) numbers