F# does not get support C# style continuations yet; I am reliably informed that it will do at some point.<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />/o:p


However if you can not wait that long it is already fair easy to get C# style continuations by implementing System.Collections.Generic.IEmunerable your self. What’s more this can be implemented as a library function then implementing your enumerator becomes just a couple of lines of code./o:p


Here is my implementation:/o:p


module Strangelights.Continuations/o:p


open System/o:p

open System.Collections/o:p

open System.Collections.Generic/o:p


// This function makes an IEnumerable interface from/o:p

// a mutable value “n” and a function “f” that takes unit and /o:p

// returns a boolean. The idea is that the function mutates/o:p

// the value return true while there are more values left/o:p

let make_enumberable n f = /o:p


    // this function creates a class that implments the /o:p

    // IEnumerator interface/o:p

    let makeenumerator() = /o:p

        { new IEnumerator<> with // implment IEnumerator itself/o:p

            get_Current() = ! n  // return value held in n/o:p

        interface IEnumerator with // implment the non generic IEnumerator/o:p

            Reset() = failwith “not implmented”/o:p

            and MoveNext() = /o:p

                f() // call the funcition we were given/o:p

            and getCurrent() = ! n :> obj // expose n with downcast to obj/o:p

        interface  IDisposable with // implment IDisposable/o:p

            Dispose() = () } in/o:p


    // now we need to implment IEnumerable to make IEnumerator/o:p

    // more friendly to our calling clients/o:p

    { new IEnumerable<> with // implment IEnumerable itself/o:p

        GetEnumerator() = make_enumerator() /o:p

    interface IEnumerable with // implment the non generic IEnumerable/o:p

        GetEnumerator() = make_enumerator() :> IEnumerator }/o:p


let make_fib_enumberable() = /o:p

    let n  = ref 0I in      // the Fibonacci number itself/o:p

    let nPlus1 = ref 1I in  // the next number in the sequence/o:p


    // function that calculates the next Fibonacci number/o:p

    // by changing the values n and nPlus1/o:p

    let get_next() =        /o:p

        let temp = ! nPlus1 in /o:p

        nPlus1 := ! n  + ! nPlus1; /o:p

        n :=  temp; /o:p

        true in/o:p


    // make the enumberator/o:p

    make_enumberable n get_next/o:p


// take the fist 40 numbers from our infinite list/o:p

let fibs = IEnumerable.take 40 (make_fib_enumberable())/o:p


// display the list/o:p

do /o:p

    fibs |> List.iter (fun i -> print_string (i.ToString()) ; print_newline());/o:p

    read_line() |> ignore/o:p