6 people like it.

Fibonacci snippet implement with Continuation

Normally, when we implement Fibonacci sequence, we use recursive function, but it will encounter the overflow exception with the big data. Below is the Fibonacci snippet with continuation, and in this way, it won't encounter the overflow exception.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
module FibContinuation = 
    let rec fib n count = 
        match n with 
        | 1 
        | 2 -> count (1) 
        | _ ->  
            let first x = 
                let second y = 
                    count(x + y) 
                fib (n-2) second 
            fib (n-1) first 
val fib : n:int -> count:(int -> 'a) -> 'a

Full name: Script.FibContinuation.fib
val n : int
val count : (int -> 'a)
val first : (int -> 'a)
val x : int
val second : (int -> 'a)
val y : int
Raw view Test code New version

More information

Link:http://fssnip.net/eU
Posted:11 years ago
Author:Jeallyn Duan
Tags: fibonacci , continuation