1 people like it.

Tail Recursive Factorial

Routine for recursively factorize integer numbers

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
let factorial n = 
    let rec fac acc n =
        if n > 0 then
            fac (acc * n) (n - 1)
        else
            acc

    fac 1 n

let summary n = 
    let rec sum acc n =
        if n > 0 then
            fac (acc + n) (n - 1)
        else
            acc

    sum 1 n
val factorial : n:int -> int

Full name: Script.factorial
val n : int
val fac : (int -> int -> int)
val acc : int
val summary : n:int -> int

Full name: Script.summary
val sum : ('a -> int -> 'a)
val acc : 'a
Raw view Test code New version

More information

Link:http://fssnip.net/7WC
Posted:5 years ago
Author:Dr. Martin Lockstrom
Tags: #factorization