# Elm basics

Andy Balaam
artificialworlds.net/blog

# Values - numbers

```> 45 45 : number > 45.3 45.3 : Float > round 45 45 : Int ```

# Values - strings

```> "foo" "foo" : String > 'x' 'x' : Char ```

# Values - booleans

```> True True : Bool > False False : Bool ```

# Values - lists

```> ["a","d","x"] ["a","d","x"] : List String > List.range 1 4 [1,2,3,4] : List number > [] [] : List a ```

# Values - tuples

```> (1,2,3) (1,2,3) : ( number, number', number'' ) > ("a",2,'x') ("a",2,'x') : ( String, number, Char ) ```

# Values - records

```> { name="Andy", score=10, comments=["foo","bar"] } { name = "Andy", score = 10, comments = ["foo","bar"] } : { comments : List String , name : String , score : number } ```

# Combining values - arithmetic

```> 3 + 4 7 : number > 3 * 4 12 : number > ( ( 3 - 4 ) / 2 ) ^ 5 -0.03125 : Float ```

# Combining values - strings

```> "Dev" ++ "Ops" "DevOps" : String ```

# Combining values - logic

```> 4 < 5 && 5 < 6 True : Bool > not False True : Bool > 6 >= 6 || 80 == 8 True : Bool > 5 /= 6 True : Bool ```

# Combining values - lists

Join 2 lists:

```> [1,2] ++ [4,3] [1,2,4,3] : List number```

# Combining values - lists

Add to the front of a list:

```> 1 :: []  : List number > 1 :: [5,4,3] [1,5,4,3] : List number ```

# Combining values - if

```if legs == 3 then "tripod" else if eyes == 1 then "cyclops" else "human" ```

# Combining values - case

```case legs of 3 -> "tripod" 4 -> "lion" _ -> "human" ```

Note: case is indentation sensitive

# Combining values - case

```case List.head mylist of Just x -> x Nothing -> "The list was empty!" ```

# Combining values - records

```> person = { name="Andy", age=21 } { name = "Andy", age = 21 } : { age : number, name : String } > { person | age=22 } { name = "Andy", age = 22 } : { name : String, age : number } ```

# Naming values

```> x = 3 3 : number > x + 2 5 : number ```

# Functions

```square n = n ^ 2 gravity v t = v - 9.81 * t ```

# Functions

```> square 3 9 : number > gravity 10 0.1 9.019 : Float > square (gravity 10 0.1) 81.342361 : Float ```

# Functions

```> square gravity 10 0.1 -- TYPE MISMATCH --------------- repl-temp-000.elm Function `square` is expecting 1 argument, but was given 3. 4│ square gravity 10 0.1 ^^^^^^ Maybe you forgot some parentheses? Or a comma?```

# Functions - anonymous

```> sos = \x y -> x^2 + y^2 : number -> number -> number > sos 3 4 25 : number ```

# Combining functions

```square n = n^2 apply_twice f n = f (f n) > apply_twice square 3 81 : number ```

# Combining functions - currying

```square_twice = apply_twice square > square_twice 3 81 : number ```

# Combining functions - map

```> List.map square [1,2,3] [1,4,9] : List number > List.map (\x -> 2*x) [1,2,3] [2,4,6] : List number ```

# Types

```x : Int x = 3 ```

# Types

```x : Int x = "a" TYPE MISMATCH The type annotation for `x` does not match its definition. 6| x : Int The type annotation is saying: Int But I am inferring that the definition has this type: String ```

# Types - records

```p : { name : String, age : Int } p = { name = "Andy", age = 21 } ```

# Types - functions

```square : Int -> Int square n = n^2 ```

# Types - functions

```gravity : Float -> Float -> Float gravity v t = v - 9.81 * t ```

# Types - functions

```square : Int -> Int square x = x^2 apply_twice : ( Int -> Int ) -> Int -> Int apply_twice f n = f (f n) square_twice : Int -> Int square_twice = apply_twice square ```

# Types - naming types

```type alias Ducks = Int type alias Elephants = Int x : Ducks x = 3 y : Elephants y = 4 > x * y 12 ```

# Types - naming types

```type alias Person = { name : String , age : Int , scores : List Int } twice_age : Person -> Int twice_age p = p.age * 2 ```

# Types - record specs

```bad_commit : { foo | lines : Int } -> Bool bad_commit c = c.lines > 100 > bad_commit { files=["a.cpp","b.elm"], lines=200 } True ```

# Types - case of names

• Type names start with a capital (e.g. Int)
• Generic param names start with lowercase (e.g. List a)

# Types - generic types

```repeats : List a -> List a repeats xs = case xs of h :: t -> h :: h :: (repeats t) [] -> [] > repeats [1,2,3] [1,1,2,2,3,3] ```

# Combining types

```type Drink = Coffee | Tea enjoy : Drink -> String enjoy drink = case drink of Coffee -> "whoosh" Tea -> "aaah" ```

# Combining types

```type Status = Available | Busy String | Away Int > a = Available Available : Repl.Status > b = Busy "in a meeting" Busy ("in a meeting") : Repl.Status > w = Away 13 Away 13 : Repl.Status ```

# Combining types

```report : Status -> String report status = case status of Available -> "is available now" Busy msg -> "is " ++ msg Away m -> "back in " ++ (toString m) ++ " mins" > status b "is in a meeting" : String > status w "back in 13 mins" : String ```

```-- Single-line comment {- multi-line comments {- nestable -} -} ```