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 890133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:40:46+00:00 2026-05-15T13:40:46+00:00

we are trying to build the Haskell-MaybeMonad sample from http://www.haskell.org/all_about_monads/html/maybemonad.html in F#. The idea

  • 0

we are trying to build the Haskell-MaybeMonad sample from http://www.haskell.org/all_about_monads/html/maybemonad.html in F#.

The idea is to search for a mailaddress in two dictionaries. If one of the both lookups returns a result we look into the third one.

let bindM x k =
    match x with
    | Some value -> k value
    | None   -> None

let returnM x = Some x

type MaybeBuilder() =
     member this.Bind(x, k) = bindM x k
     member this.Return(x)  = returnM x
     member this.ReturnFrom(x) = x
     member this.Delay(f)   = f()

let maybe = MaybeBuilder()

//Sample dictionaries
let fullNamesDb = 
    [("Bill Gates", "billg@microsoft.com")    
     ("Bill Clinton", "bill@hope.ar.us")
     ("Michael Jackson", "mj@wonderland.org")
     ("No Pref Guy", "guy@nopref.org")]
       |> Map.ofList

let nickNamesDb =
    [("billy", "billg@microsoft.com")
     ("slick willy", "bill@hope.ar.us")
     ("jacko", "mj@wonderland.org") ]
        |> Map.ofList

let prefsDb =
    [("billg@microsoft.com", "HTML")
     ("bill@hope.ar.us", "Plain")
     ("mj@wonderland.org", "HTML")]
        |> Map.ofList


let mplus m1 m2 = if m1 <> None then m1 else m2
let (+) = mplus

let lookUp name = maybe {
    let! combined = fullNamesDb.TryFind name + nickNamesDb.TryFind name
    return! prefsDb.TryFind combined
}

let billGatesPref = lookUp "Bill Gates" |> printfn "%A" // Some "HTML"
let billyPref = lookUp "billy" |> printfn "%A" // Some "HTML"
let billClintonPref = lookUp "Bill Clinton" |> printfn "%A" // Some "Plain"
let steffenPref = lookUp "Steffen" |> printfn "%A" // None
let noPref = lookUp "No Pref Guy" |> printfn "%A" // None

System.Console.ReadKey() |> ignore

The problem is that we perform the second lookup even if the first one returns a result. The nice thing about Haskell is here, that it evaluates lazy. Now we looking for something similar in F#. We tried the following but it looks ugly and seems to break the idea of encapsulating the maybe logic in the builder:

let mplus m1 m2 = if m1 <> None then m1 else m2()
let (+) = mplus

let lookUp name = maybe {
    let! combined = fullNamesDb.TryFind name + fun _ -> nickNamesDb.TryFind name
    return! prefsDb.TryFind combined
}

Is there a better solution?

Regards,
forki

  • 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-15T13:40:47+00:00Added an answer on May 15, 2026 at 1:40 pm

    You can implement additional methods Run/Combine in MaybeBuilder so result shall be the following:

    let bindM x k =
    match x with
    | Some value -> k value
    | None   -> None
    
    let returnM x = Some x
    
    type MaybeBuilder() =
        member this.Bind(x, k) = bindM x k
        member this.Return(x)  = returnM x
        member this.ReturnFrom(x) = x
        member this.Delay(f)   = f
        member this.Combine(a, b) = if Option.isSome a then a else b()
        member this.Run(f) = f()
    
    let maybe = MaybeBuilder()
    
    //Sample dictionaries (the same with original sample)
    let fullNamesDb = ... 
    let nickNamesDb = ...
    let prefsDb = ....
    
    let lookUp name = 
        let findName m = maybe {
            let! v = Map.tryFind name m
            return! prefsDb.TryFind v 
            }
    
        maybe {
            return! findName fullNamesDb
            return! findName nickNamesDb 
        }
    
    
    let billGatesPref = lookUp "Bill Gates" |> printfn "%A" // Some "HTML"
    let billyPref = lookUp "billy" |> printfn "%A" // Some "HTML"
    let billClintonPref = lookUp "Bill Clinton" |> printfn "%A" // Some "Plain"
    let steffenPref = lookUp "Steffen" |> printfn "%A" // None
    let noPref = lookUp "No Pref Guy" |> printfn "%A" // None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but
I'm trying build a method which returns the shortest path from one node to
Trying to build my project with ANT in idea 10 and I get a
I'm trying to build a simple lexer/parser with Alex/Happy in Haskell, and I would
I'm trying to create a shared library from Haskell source code. I've tried following
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
I am trying to build a smallish haskell app that will translate a few
Trying to build a proxy server. recv from client -> send to server ->
Trying to build Wireshark from source as there is no Linux installer and I
I'm currently trying to build wxHaskell as described in the Haskell wiki here .

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.