functions.cd 591 Bytes
Newer Older
1
(* Simple functions can be defined this way: *)
2 3 4
let f1 (x : Int) : Int = x + 3
;;
f1 5
5 6

(* With several arguments: *)
7 8 9
let f2 (x : Int, y : Int) : Int = x + y
;;
f2 (10,20)
10 11

(* You may directly deconstruct the arguments: *)
12 13 14
type A = <a href=String>String
let f3 (<a href=url>txt : A) : String = url @ "=>" @ txt
;;
15 16 17 18 19 20
f3 <a href="http://www.cduce.org">"CDuce homepage";;

(* In general, if you want to specify several arrow types, or
   use several pattern matching branches, you have the general
   form: *)

21
let f4 (A -> String; ['0'--'9'+] -> Int)
22
| x & A -> f3 x
23 24 25
| x -> int_of x
;;
f4 "123"