Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
(* to understand recursion we must first .... understand recursion! *) fun tab() = print("\t"); (* print tab *) fun nl() = print("\n"); (* print newline *) fun pi(n) = print(Int.toString(n)); (* print integer *) fun f91(x) = (pi(x);tab(); (* print actual x for every call *) if x>100 then x-10 else f91(f91(x+11))); (* f91(100); 100 111 101 val it = 91 : int *) (* f91(78); 78 89 100 111 101 91 102 92 103 93 104 94 105 95 106 96 107 97 108 98 109 99 110 100 111 101 91 102 92 103 93 104 94 105 95 106 96 107 97 108 98 109 99 110 100 111 101 val it = 91 : int *) (* What is happening here? We have f91(x) = x-10 if x > 100. Otherwise f91(x) = f91(f91(x+11)) = f91(x+11-10) = f91(x+1) =...= f91(101) = 91 provided x+11 > 100, that is, f91(x) = 91 if 89 < x <= 101. In particular f91(91) = 91. It is now easy to see that f91(x) = 91 for all x <= 100. *) (* depth n of call stack + actual x printed at each call: fun f(n,x) = (pi(n);tab();pi(x);nl(); (* n counts f's to the left *) if x>100 then x-10 else f(n,f(n+1,x+11))); *)