末尾再帰たんの行方を探ってみた

(* 再帰的な階乗の定義(F#) *)
let rec fact = function
    0 -> 1 | n -> n * fact (n-1)

コンパイルしてデコンパイル

public static int fact(int _arg1)
{
    switch (_arg1)
    {
        case 0:
            return 1;
    }
    return (_arg1 * fact(_arg1 - 1));
}

ま  さ  に  直  訳



(* 末尾再帰的な階乗の定義(F#) *)
let rec fact2 n res =
    if n = 0 then res else fact2 (n-1) (n*res)

コンパイルしてデコンパイル

public static int fact2(int n, int res)
{
    while (n != 0)
    {
        res = n * res;
        n--;
    }
    return res;
}

こいつぁすげぇや!