-- FunctionsAndLet.daml
module FunctionsAndLet where
-- ── Basic function ──────────────────────────────────────
add : Int -> Int -> Int
add x y = x + y
-- Partial application: fix one argument.
addFive : Int -> Int
addFive = add 5
result : Int
result = addFive 3 -- 8
-- ── let / in ────────────────────────────────────────────
-- let binds local names; `in` introduces the body expression.
circleArea : Decimal -> Decimal
circleArea r =
let
pi = 3.14159265358979
rSquared = r * r
in
pi * rSquared
-- ── Guards ──────────────────────────────────────────────
-- Guards are conditions checked top to bottom.
-- `otherwise` is an alias for True and always matches.
classify : Int -> Text
classify n
| n < 0 = "negative"
| n == 0 = "zero"
| n < 100 = "small positive"
| otherwise = "large positive"
-- ── Pattern matching ────────────────────────────────────
data Shape = Circle Decimal | Rectangle Decimal Decimal
deriving (Show)
area : Shape -> Decimal
area (Circle r) = 3.14159265358979 * r * r
area (Rectangle w h) = w * h
-- ── Lambdas ─────────────────────────────────────────────
-- Backslash introduces a lambda. Arrow separates params from body.
double : [Int] -> [Int]
double xs = map (\x -> x * 2) xs
-- Equivalent using a named helper:
doubleItem : Int -> Int
doubleItem x = x * 2
double2 : [Int] -> [Int]
double2 xs = map doubleItem xs
-- ── Standard list combinators ────────────────────────────
evens : [Int] -> [Int]
evens xs = filter (\x -> x % 2 == 0) xs
total : [Decimal] -> Decimal
total xs = foldl (+) 0.0 xs
-- map applies a function to every element
-- filter keeps elements satisfying a predicate
-- foldl reduces a list to a single value (left-associative)