Don Box/Sam Ruby on Continuations

The ever charismatic Mr. Box made a lovely post about continuations in C# 2.0:

http://pluralsight.com/blogs/dbox/archive/2005/04/17/7467.aspx

 

Because I have used a simpler Fibonacci implementation in couple of places, I thought I’d port it to F#.

 

Of course continuations are not a new idea; they have been used for a long time in ML style programming. The implementation is most like the continuations by anonymous methods, as anonymous methods work well in ML as they are very similar idea to lambdas.

 

Here is the source:

 

 

let fib() =

      let i = ref 0 in

      let j = ref 1 in

      let cont() = (

                let result = !i in

            i := !j;

            j := result + !j;

            result

            ) in

      cont

 

let _ = let x = ref 0 in

      let f = fib() in

      while !x < 1000 do

            x := f();

            Printf.printf "%d\n" !x;

      done;

      ()

Bookmark
dotnetkicks+, digg+, reddit+, del.icio.us+, dzone+, facebook+

Print | posted @ Saturday, April 23, 2005 4:04 PM

Comments on this entry:

No comments posted yet.

Your comment:

(Note: all comments are moderated so it may take sometime to appear)

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 5 and 5 and type the answer here:
 

Links

 Subscribe in a reader
Twitter Follow me on Twitter
FaceBook View my Facebook
LinkedIn View my LinkedIn Profile Viadeo Viadeo Profile (Français)

Badges



Disclaimer

The views expressed on this weblog are mine and do not necessarily reflect the views of my employer.

All postings are provided "AS IS" with no warranties, and confer no rights.

www.flickr.com
This is a Flickr badge showing public photos and videos from Robert Pickering. Make your own badge here.