I am trying to implement a simple range function in OCaml, and I was wondering how I can do this for different arity calls.
let range_aux ~start ~stop ~step =
let rec aux start stop step acc =
match (start, stop, step, acc) with
| (start,stop,step,acc) when start = stop -> List.rev acc
| (start,stop,step,acc) -> aux (start + step) stop step (start :: acc) in
aux start stop step []
let range ~start ~stop ~step = range_aux ~start ~stop ~step
let range ~stop ~step = range_aux ~start:0 ~stop ~step
let range ~stop = range_aux ~start:0 ~stop ~step:1
This obviously does not work when the last definition wins. Is there a way to define multiple arity functions?
source
share