how do I count the amount of times a (recursive) function executes itself in ocaml?

Viewed 75

needing some help (if possible) in how to count the amount of times a recursive function executes itself. I don't know how to make some sort of counter in OCaml. Thanks!

3 Answers

Let's consider a very simple recursive function (not Schroder as I don't want to do homework for you) to calculate Fibonacci numbers.

let rec fib n =
  match n with
  | 0 | 1 -> 1
  | _ when n > 0 -> fib (n - 2) + fib (n  - 1)
  | _ -> raise (Invalid_argument "Negative values not supported")

Now, if we want to know how many times it's been passed in, we can have it take a call number and return a tuple with that call number updated.

To get each updated call count and pass it along, we explicitly call fib in let bindings. Each time c shadows its previous binding, as we don't need that information.

let rec fib n c =
  match n with
  | 0 | 1 -> (1, c + 1)
  | _ when n > 0 -> 
    let n',  c = fib (n - 1) (c + 1) in
    let n'', c = fib (n - 2) (c + 1) in
    (n' + n'', c)
  | _ -> raise (Invalid_argument "Negative values not supported")

And we can shadow that to not have to explicitly pass 0 on the first call.

let fib n = fib n 0

Now:

utop # fib 5;;
- : int * int = (8, 22)

The same pattern can be applied to the Schroder function you're trying to write.

You can create a reference in any higher scope like so

let counter = ref 0 in
let rec f ... =
  counter := !counter + 1;
  ... (* Function body *)

If the higher scope happens to be the module scope (or file top-level scope) you should omit the in

You can return a tuple (x,y) where y you increment by one for each recursive call. It can be useful if your doing for example a Schroder sequence ;)

Related