Currying in OCaml
Every function takes one argument; partial application is free.
(* Every function takes one argument; more means curried. *)
let record pool species =
Printf.sprintf "%s spotted in the %s pool" species pool
(* Partial application bakes in the first argument. *)
let at_north = record "north"
let () =
print_endline (at_north "anemone");
print_endline (at_north "limpet");
print_endline (record "ledge" "sea star")
(* Annotations are legal but inference usually carries the day. *)
let quadrats (pools : int) : int = pools * 4
let () = Printf.printf "Quadrats: %d\n" (quadrats 6)
How it works
record pool speciesis two nested one-argument functions.record "north"bakes in the pool and returns the rest.- Annotations like
(pools : int)are legal; inference usually wins.
Keywords and builtins used here
intlet
The run, in numbers
- Lines
- 15
- Characters to type
- 517
- Tokens
- 85
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 78 seconds.
Step 1 of 4 in Functions, step 4 of 29 in Language basics.