import Html
greet str = Html.text str
main =
greet "Hello Hamburg!"
Result
Type Annotations
Elm
import Html exposing (Html)
greet : String -> Html
greet str =
Html.text str
main : Html
main =
greet "Hello Hamburg!"
Result
Parameter Types & Return Types
Elm
import Html exposing (Html)
import String
greet : Int -> String -> Html
greet number string =
Html.text
(String.repeat number string)
main : Html
main =
greet 3 "Hello Hamburg! "
Result
Function Application aka Data Pipelines
Elm
greet : Int -> String -> Html
greet number string =
string
|> String.repeat number
|> Html.text
main : Html
main =
greet 3 "Hello Hamburg! "
Result
Functional Reactive Programming
in Elm
Signals
Elm
import Html exposing (Html)
import Mouse
toHtml : Int -> Html
toHtml x =
Html.text (toString x)
main : Signal Html
main =
Signal.map toHtml Mouse.x
function repeatFirst(list) {
var repeated = list[0].repeat(3);
alert(repeated);
}
var list1 =
[ 'hip', 'hop', 'hooray!' ];
...
repeatFirst(list1);
var list2 = [];
...
repeatFirst(list2);
Compile Time > Runtime
repeatFirst list =
let elem = List.head list
in String.repeat 3 elem
> elm-make RepeatFirst.elm --output repeat-first.html
-- TYPE MISMATCH -------------------------- RepeatFirst.elm
The 2nd argument to function repeat is causing a mismatch.
8│ String.repeat 3 elem
^^^^
Function `repeat` is expecting the 2nd argument to be:
String
But it is:
Maybe a
Hint: I always figure out the type of arguments from left
to right. If an argument is acceptable when I check it, I
assume it is "correct" in subsequent checks. So the problem
may actually be in how previous arguments interact with the
2nd.
Corrected Version
repeatFirst list =
let elem = List.head list |> Maybe.withDefault ""
in String.repeat 3 elem