Currying in F#
Every function takes one argument; partial application is free.
// Every function takes one argument; more arguments mean curry.
let feed syrupLiters hive =
sprintf "%s gets %.1f L of syrup" hive syrupLiters
// Partial application bakes in the first argument.
let autumnFeed = feed 2.5
printfn "%s" (autumnFeed "Willow")
printfn "%s" (autumnFeed "Bramble")
// Type annotations are allowed but inference usually wins.
let addSuper (frames: int) hive =
sprintf "%s grows to %d frames" hive (frames + 10)
printfn "%s" (addSuper 8 "Clover")
How it works
feed syrupLiters hiveis really two nested functions.feed 2.5bakes in the first argument and returns the rest.- Annotations like
(frames: int)steer inference when needed.
Keywords and builtins used here
let
The run, in numbers
- Lines
- 13
- Characters to type
- 474
- Tokens
- 71
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 61 seconds.
Step 1 of 3 in Functions, step 4 of 27 in Language basics.