首先,我对你重新试图与反面做些什么感到困惑不解,你们是否希望它达到123? 意味着:
while (count < 123) { count++; someFunction(count); }
......意味着,在达到123岁以前,你需要多少时间才能离开。
如果你想要保留多少职能,直到达到某种限制,那么你就可采用这样的办法:
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 () ;;
如果你想避免 state的国家(一般是一个好主意),那么你可以这样做,而不会 re:
let someFunction n =
let rec aux count =
if count >= n then count
else aux (count+1) in
aux 0 ;;
或许这是你试图做些什么?
let someOtherFunction n =
Printf.printf "n is: %d
" 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
而另一方面,如果你想要跟踪多少次职能,那么你就不得不在与某些职能定义相同的程度上抵制,例如:
let count = ref 0 ;;
let rec someFunction n f =
if !count >= 123 then count
else (
incr count;
f n;
someFunction n f
) ;;