Reading — step 1 of 5
Learn
~1 min readFunctions
Functions are bindings whose value is a function:
let square x = x * x
square 5 // 25 (no parens needed for application)
Multiple parameters are space-separated:
let add x y = x + y
add 3 4 // 7
This is curried by default — add 3 returns a function expecting one more arg:
let add5 = add 5
add5 10 // 15
Anonymous functions use fun:
let double = fun x -> x * 2
let add = fun x y -> x + y
Multi-line bodies use indentation:
let describe n =
let absN = abs n
let sign = if n >= 0 then "+" else "-"
sprintf "%s%d" sign absN
The last expression is the return value. No return keyword.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…