Elm (programming language)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking.

Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking. For example, imagine an application that displays a number and a button that increments the number when pressed. In this case, all we need to store is one number, so our model can be as simple as type alias Model = Int .

How it works

The view function would be defined with the Html library and display the number and button. For the number to be updated, we need to be able to send a message to the update function, which is done through a custom type such as type Msg = Increase . The Increase value is attached to the button defined in the view function such that when the button is clicked by a user, Increase is passed on to the update function, which can update the model by increasing the number. In the Elm Architecture, sending messages to update is the only way to change the state.

In more sophisticated applications, messages may come from various sources: user interaction, initialization of the model, internal calls from update , subscriptions to external events (window resize, system clock, JavaScript interop...) and URL changes and requests. -- This is a single line comment. {- This is a multi-line comment. It is {- nestable. -} -} -- Here we define a value named `greeting`.

The type is inferred as a `String`. greeting = "Hello World!" -- It is best to add type annotations to top-level declarations. hello : String hello = "Hi there." -- Functions are declared the same way, with arguments following the function name. add x y = x + y -- Again, it is best to add type annotations. hypotenuse : Float -> Float -> Float hypotenuse a b = sqrt (a^2 + b^2) -- We can create lambda functions with the `\[arg] -> [expression]` syntax. hello : String -> String hello = \s -> "Hi, " ++ s -- Function declarations may have the anonymous parameter names denoted by `_`, -- which are matched but not used in the body. const : a -> b -> a const k _ = k -- Functions are also curried; here we've curried the multiplication -- infix operator with a `2` multiplyBy2 : number -> number multiplyBy2 = (*) 2 -- If-expressions are used to branch on `Bool` values absoluteValue : number -> number absoluteValue number = if number < 0 then negate number else number -- Records are used to hold values with named fields book : { title : String, author : String, pages : Int } book = { title = "Steppenwolf" , author = "Hesse" , pages = 237 } -- Record access is done with `.` title : String title = book.title -- Record access `.` can also be used as a function author : String author = .author book -- We can create tagged unions with the `type` keyword. -- The following value represents a binary tree. type Tree a

Text derived from https://en.wikipedia.org/w/index.php?oldid=1378655114 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

39 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.