= robust apps
Goodbye OOP
No magic
More on that later
-- Function definition
sayHello name =
"Hello " ++ name ++ "!"
-- Function call
sayHello "Liip"
-- Variables
nameLength name =
let
nameLengthStr = String.fromInt (String.length name)
in
"Your name " ++ name ++ " is " ++ nameLengthStr ++ " characters long."
-- Lists
List.map nameLength [ "Alice", "Bob", "Cédric" ]
-- Conditions
isAllowedToDrive name vehicleType =
name == "Cédric" && vehicleType == "bulldozer"
if isAllowedToDrive "Cédric" "car" then
"Go ahead."
else
"You're not allowed to drive"
-- Type safety: compiler error
isAllowedToDrive "Cédric" 27
-- Type safety: compiler error
nameLength name =
let
nameLengthStr = String.length name
in
"Your name " ++ name ++ " is " ++ nameLengthStr ++ " characters long."
-- Annotations
isAllowedToDrive : String -> String -> Bool
isAllowedToDrive name vehicleType =
name == "Cédric" && vehicleType == "bulldozer"
-- Types
type VehicleCategory
= Bulldozer
| Car
| Truck
| Motorcycle
-- Pattern matching
isAllowedToDrive : String -> VehicleCategory -> Bool
isAllowedToDrive name vehicleCategory =
case vehicleCategory of
Bulldozer ->
return name == "Cédric"
-- Omitting this would result in a compiler error
_ ->
False
module Main exposing ( main )
import Browser
import Html exposing ( div, button, text )
import Html.Events exposing ( onClick )
type Msg
= Increment
| Decrement
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, text (String.fromInt model)
, button [ onClick Increment ] [ text "+" ]
]
main =
Browser.sandbox { init = 0, update = update, view = view }
1
2
3
4
5
guide.elm-lang.org
ellie-app.com
elmprogramming.com
elmlang.slack.com