Why do Seq.fold_left and Seq.iter recurse on a closure rather than at the first level?

Here are their definitions (from here):

let fold_left f acc seq =
  let rec aux f acc seq = match seq () with
    | Nil -> acc
    | Cons (x, next) ->
        let acc = f acc x in
        aux f acc next
  in
  aux f acc seq

let iter f seq =
  let rec aux seq = match seq () with
    | Nil -> ()
    | Cons (x, next) ->
        f x;
        aux next
  in
  aux seq

I would have imagined they’d be implemented directly as their inner functions are implemented, like this:

let rec fold_left f acc seq = match seq () with
  | Nil -> acc
  | Cons (x, next) ->
      let acc = f acc x in
      fold_left f acc next

let rec iter f seq = match seq () with
  | Nil -> ()
  | Cons (x, next) ->
      f x;
      iter f next

I’m pretty new at OCaml, so I’m trying to understand if I’m overlooking a behavioral difference, or if it’s to do with the style of idiomatic OCaml (or something else entirely). Thanks!

I don’t recall exactly, but it’s probably just my personal style. In general it’s nice to have an inner function when there are additional arguments such as accumulators.

Cool, that makes sense. I was hoping it was that, and not that I was misunderstanding the code. Thanks!