Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:55:33+00:00 2026-05-28T22:55:33+00:00

Here is what I have so far: type Maybe<‘a> = option<‘a> let succeed x

  • 0

Here is what I have so far:

type Maybe<'a> = option<'a>

let succeed x = Some(x)

let fail = None

let bind rest p =
    match p with
        | None -> fail
        | Some r -> rest r

let rec whileLoop cond body =
    if cond() then
        match body() with
        | Some() ->
            whileLoop cond body
        | None ->
            fail
    else
        succeed()

let forLoop (xs : 'T seq) f =
    using (xs.GetEnumerator()) (fun it ->
            whileLoop
                (fun () -> it.MoveNext())
                (fun () -> it.Current |> f)
        )

whileLoop works fine to support for loops, but I don’t see how to get while loops supported. Part of the problem is that the translation of while loops uses delay, which I could not figure out in this case. The obvious implementation below is probably wrong, as it does not delay the computation, but runs it instead!

let delay f = f()

Not having delay also hinders try...with and try...finally.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:55:34+00:00Added an answer on May 28, 2026 at 10:55 pm

    There are actually two different ways of implementing continuation builders in F#. One is to represent delayed computations using the monadic type (if it supports some way of representing delayed computations, like Async<'T> or the unit -> option<'T> type as shown by kkm.

    However, you can also use the flexibility of F# computation expressions and use a different type as a return value of Delay. Then you need to modify the Combine operation accordingly and also implement Run member, but it all works out quite nicely:

    type OptionBuilder() = 
      member x.Bind(v, f) = Option.bind f v
      member x.Return(v) = Some v
      member x.Zero() = Some ()
      member x.Combine(v, f:unit -> _) = Option.bind f v
      member x.Delay(f : unit -> 'T) = f
      member x.Run(f) = f()
      member x.While(cond, f) =
        if cond() then x.Bind(f(), fun _ -> x.While(cond, f)) 
        else x.Zero()
    
    let maybe = OptionBuilder()
    

    The trick is that F# compiler uses Delay when you have a computation that needs to be delayed – that is: 1) to wrap the whole computation, 2) when you sequentially compose computations, e.g. using if inside the computation and 3) to delay bodies of while or for.

    In the above definition, the Delay member returns unit -> M<'a> instead of M<'a>, but that’s perfectly fine because Combine and While take unit -> M<'a> as their second argument. Moreover, by adding Run that evaluates the function, the result of maybe { .. } block (a delayed function) is evaluated, because the whole block is passed to Run:

    // As usual, the type of 'res' is 'Option<int>'
    let res = maybe { 
        // The whole body is passed to `Delay` and then to `Run`
        let! a = Some 3
        let b = ref 0
        while !b < 10 do 
          let! n = Some () // This body will be delayed & passed to While
          incr b
        if a = 3 then printfn "got 3"
        else printfn "got something else"
        // Code following `if` is delayed and passed to Combine
        return a }
    

    This is a way to define computation builder for non-delayed types that is most likely more efficient than wrapping type inside a function (as in kkm’s solution) and it does not require defining a special delayed version of the type.

    Note that this problem does not happen in e.g. Haskell, because that is a lazy language, so it does not need to delay computations explicitly. I think that the F# translation is quite elegant as it allows dealing with both types that are delayed (using Delay that returns M<'a>) and types that represent just an immediate result (using Delay that returns a function & Run).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total newb to LINQ. Here is the code I have so far:
Here is what I have tried thus far: > cd /Applications/MAMP/bin/php5/bin/ > ./pear channel-discover
I really wonder why this question hasn't popped up here so far. I have
Just wondering what little scripts/programs people here have written that helps one with his
Since upgrading to 2008 I and many people here have noticed that randomly VS
Here I have: Public Structure MyStruct Public Name as String Public Content as String
Here I have a password field: /*********************PASSWORD**********************/ $password = new Zend_Form_Element_Password('password'); $alnumValidator = new
Here I have an arbitrary IEnumerable<T> . And I'd like to page it using
Around here we have been working with a bunch of Visual Source Safe repositories
enter code here I have a table on SQL server 2005 with bigint primary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.