Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 531

Tail call optimization with a function returning a tuple

$
0
0

I have a simple function that splits a list at an index:

let rec split_at ls i =  match i with  | 0 -> ([], ls)  | _ ->    match ls with    | [] -> raise Not_found    | h::t ->      match split_at t (i - 1) with      | (left, right) -> ((h :: left), right)

Is there a way to get the OCaml compiler to optimize this function to use constant stack space?

I have tried using @tail_mod_cons but it doesn't work. I understand that the call is not really in tail position, but it feels like it should be optimizable.


Viewing all articles
Browse latest Browse all 531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>