Reputation: 43
I'm new to Ocaml. The problem I'm having is keeping track of the number of recursive calls in a function.
For instance, I wrote a following function:
let rec someFunction n =
let digi = someOtherFunction n in
match digi with
| x when x > 123 -> someFunction digi
| x -> [want to output # of recursive calls] ;;
How do I go about doing this? I tried creating a variable but it just keeps resetting to its initial value if I have it in someFunction. What I basically want to do is something like this:
while ( x > 123) {
count++;
someFunction(x);
}
return count;
Forgive me if this is something extremely trivial.
Upvotes: 3
Views: 4959
Reputation: 5668
There are several ways to do this. One is to do it with a while loop like you wrote but with references to allow variables to be able to change values.
let x := starting_value;
let count := 0;
while ( !x > 123) do (
count := count + 1;
x := someFunction(x)
) done;
!count
Or if you want to write purely functional code, you could add a helper function like so:
let someFunction n =
let rec someFunctionHelper n count =
let digi = someOtherFunction n in
match digi with
| x when x > 123 -> someFunctionHelper digi (count + 1)
| x -> count
in
someFunctionHelper n 0
The second way is how I would write it. Essentially, we're just replacing someFunction from your original code with an alternate version which takes an extra argument which indicates how many times it has been called so far. When we call it the first time (last line of the function) we start it at zero and then after that, we pass a number one higher than what we got each time.
Upvotes: 1
Reputation: 8913
First off, I'm a bit confused about what you're trying to do with the counter, wouldn't you want it to count up to 123? Wouldn't that imply that you need:
while (count < 123) { count++; someFunction(count); }
...meaning that you count the number of times it's called until it reaches 123 and then exits.
If you want to keep count of how many times the function has been called up till a certain limit then you could use a ref like this:
let someFunction n =
let count = ref 0 in
let rec aux () =
if !count >= n then count
else (
incr count;
(* do the stuff you wanted to do in someFunction here *)
aux () ) in
aux () ;;
If you want to avoid mutable state (generally a good idea) then you could do this without a ref:
let someFunction n =
let rec aux count =
if count >= n then count
else aux (count+1) in
aux 0 ;;
Perhaps this is what you're trying to do?:
let someOtherFunction n =
Printf.printf "n is: %d\n" n;;
let someFunction n f =
let rec aux count =
if count >= n then count
else (
f count;
aux (count+1)
) in
aux 0 ;;
# someFunction 10 someOtherFunction ;;
n is: 0
n is: 1
n is: 2
n is: 3
n is: 4
n is: 5
n is: 6
n is: 7
n is: 8
n is: 9
- : int = 10
If, on the other hand, you want to keep track of how many times someFunction is called then you'll need that ref counter at the same scope level as the someFunction definition, something like:
let count = ref 0 ;;
let rec someFunction n f =
if !count >= 123 then count
else (
incr count;
f n;
someFunction n f
) ;;
Upvotes: 2