01
Getting Started
Hello World
Elm apps start with a main function.
elm
module Main exposing (main)
main =
text "Hello, World!"Types
Type aliases define record shapes.
elm
type alias Person =
{ name : String
, age : Int
}
p = Person "Alice" 30Functions
Every function is curried; types use -> notation.
elm
add : Int -> Int -> Int
add a b =
a + bElm Architecture
The Elm Architecture: Model-View-Update pattern.
elm
-- Model
type alias Model = { count : Int }
-- Update
type Msg = Increment | Decrement
update msg model =
case msg of
Increment -> { model | count = model.count + 1 }
Decrement -> { model | count = model.count - 1 }02
Data Structures
Lists
List module has map, filter, foldl, etc.
elm
nums = [1, 2, 3]
List.map (\n -> n * 2) nums
List.filter (\n -> n > 1) numsMaybe
Maybe handles null safely — Just or Nothing.
elm
type Maybe a = Just a | Nothing
safeDiv a b =
if b == 0 then Nothing else Just (a // b)Result
Result is for error handling — Ok or Err.
elm
type Result e a = Ok a | Err e
parse s =
if String.length s > 0 then Ok s else Err "empty"Records
Record access with . and update with |.
elm
p = { name = "Alice", age = 30 }
p.name
{ p | age = 31 } -- updateWas this helpful?