I am learning OCaml coming from F#. In F#, to calculate the average of an array whose element type supports addition and division, one can write
let inline average (arr: 'a) : 'a when ^a : (static member DivideByInt : ^a * int -> ^a) and ^a : (static member (+) : ^a * ^a -> ^a) and ^a : (static member Zero : ^a) = if Array.length arr = 0 then (LanguagePrimitives.GenericZero) else LanguagePrimitives.DivideByInt (Array.fold (+) (LanguagePrimitives.GenericZero) arr) (Array.length arr)
My understanding is that in OCaml, one would have a module type like so:
module type Averagable = sig type 'a t val divide_by_int : 'a -> int -> 'a val plus : 'a -> 'a -> 'a val zero : 'a end
My question is how the corresponding function would be written:
let average arr = ???