Die Syntax von Labels in OCaml
Eine Sache, die ich immer wieder nachschlagen muss, ist die Syntax der Labels, also benamte und/oder optionale Funktionsparameter.
Benamte Parameter
Implementierung:
let name ~first ~last =
first ^ " " ^ last
Signatur:
val name : first:string -> last:string -> string
Aufruf:
name ~last:"Torvalds" ~first:"Linus" (* "Linus Torvalds" *)
name "Linus" "Torvalds" (* "Linus Torvalds" *)
Optionale Parameter
Implementierung:
let increase ?(by=1) num =
num + by
Signatur:
val increase : ?by:int -> int -> int
Aufruf:
increase 10 (* 11 *)
increase ~by:2 10 (* 12 *)
Für mehr Beispiele und Details siehe Labels - Labelled and optional arguments to functions.