Expand a tuple in OCaml

Is there a way to apply a function to a tuple of elements as arguments to a function? Or, if not, can I somehow create a function with an arbitrary number of arguments and apply some other function to the "tail" in its body, as would its arguments?

+5
source share
2 answers

Generally not. In case 2, the arguments you can use the functions curryand uncurryto the extended battery for Pervasives .

Perhaps something can be prepared using the module Obj, as well as inside printf, but I will stay far from it. The difficulty is that the type system does not give you a way to express the type of a generalized function curryor uncurry. The type system does not allow you to “calculate” the length of a tuple - a 2-tuple is a 2-tuple, and you have no way to express what is (a*b*c)really (a*b)with the optional component. printfhas special support from the compiler to make the types valid, and this leads to the fact that the function type is part of the format type (therefore, similar solutions will not work for tuples).

+7
source

The language itself does not allow you to define a function on tuples of arbitrary size.

, ( SML, OCaml).

+7

All Articles