> 45
45 : number
> 45.3
45.3 : Float
> round 45
45 : Int
> "foo"
"foo" : String
> 'x'
'x' : Char
> True
True : Bool
> False
False : Bool
> ["a","d","x"]
["a","d","x"] : List String
> List.range 1 4
[1,2,3,4] : List number
> []
[] : List a
> (1,2,3)
(1,2,3) : ( number, number', number'' )
> ("a",2,'x')
("a",2,'x') : ( String, number, Char )
> { name="Andy", score=10, comments=["foo","bar"] }
{ name = "Andy", score = 10, comments = ["foo","bar"] }
: { comments : List String
, name : String
, score : number }
> 3 + 4
7 : number
> 3 * 4
12 : number
> ( ( 3 - 4 ) / 2 ) ^ 5
-0.03125 : Float
> "Dev" ++ "Ops"
"DevOps" : String
> 4 < 5 && 5 < 6
True : Bool
> not False
True : Bool
> 6 >= 6 || 80 == 8
True : Bool
> 5 /= 6
True : Bool
Join 2 lists:
> [1,2] ++ [4,3]
[1,2,4,3] : List number
Add to the front of a list:
> 1 :: []
[1] : List number
> 1 :: [5,4,3]
[1,5,4,3] : List number
if legs == 3 then
"tripod"
else if eyes == 1 then
"cyclops"
else
"human"
case legs of
3 -> "tripod"
4 -> "lion"
_ -> "human"
Note: case is indentation sensitive
case List.head mylist of
Just x -> x
Nothing -> "The list was empty!"
> person = { name="Andy", age=21 }
{ name = "Andy", age = 21 } : { age : number, name : String }
> { person | age=22 }
{ name = "Andy", age = 22 } : { name : String, age : number }
> x = 3
3 : number
> x + 2
5 : number
square n =
n ^ 2
gravity v t =
v - 9.81 * t
> square 3
9 : number
> gravity 10 0.1
9.019 : Float
> square (gravity 10 0.1)
81.342361 : Float
> 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?
> sos = \x y -> x^2 + y^2
: number -> number -> number
> sos 3 4
25 : number
square n = n^2
apply_twice f n = f (f n)
> apply_twice square 3
81 : number
square_twice = apply_twice square
> square_twice 3
81 : number
> List.map square [1,2,3]
[1,4,9] : List number
> List.map (\x -> 2*x) [1,2,3]
[2,4,6] : List number
x : Int
x = 3
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
p : { name : String, age : Int }
p = { name = "Andy", age = 21 }
square : Int -> Int
square n = n^2
gravity : Float -> Float -> Float
gravity v t = v - 9.81 * t
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
type alias Ducks = Int
type alias Elephants = Int
x : Ducks
x = 3
y : Elephants
y = 4
> x * y
12
type alias Person =
{ name : String
, age : Int
, scores : List Int
}
twice_age : Person -> Int
twice_age p = p.age * 2
bad_commit : { foo | lines : Int } -> Bool
bad_commit c = c.lines > 100
> bad_commit { files=["a.cpp","b.elm"], lines=200 }
True
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]
type Drink = Coffee | Tea
enjoy : Drink -> String
enjoy drink =
case drink of
Coffee -> "whoosh"
Tea -> "aaah"
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
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 -}
-}