Function declaration syntax in OCaml

I would like to define a function as follows:

let f (a: int) (b: int) (c: int) (d: int): int = ... 

Is it possible to make a signature shorter without making them a tuple? Since I still want f have 4 arguments.

Many thanks.

Edit1: I just think it's useless to repeat int 4 times and create something like let f (a, b, c, d: int): int , which is currently not allowed.

+7
source share
2 answers

Try this syntax:

 let g: int -> int -> int -> int -> int = fun abcd -> assert false 

This is not much shorter, but if you have a lot of them, you can define type arith4 = int -> int -> int -> int -> int and use this name as a type annotation for g .

+12
source

My OCaml is rusty, but I believe that you can do this by declaring your own type and unpacking it into the function body.

 type four = int*int*int*int let myfunction (t:four) = let a, b, c, d = t in a + b + c + d; 

You can also do this:

 let sum4 ((a, b, c, d):int*int*int*int) = a + b + c + d;; 
+2
source

All Articles