Implementing the Haskell-MaybeMonad in F# - how can we get this lazy?

前端 未结 2 1550
盖世英雄少女心
盖世英雄少女心 2021-02-10 04:22

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

2条回答
  •  独厮守ぢ
    2021-02-10 04:43

    There's always Lazy, which is effectively what you have here but with different syntax:

    let mplus m1 (m2 : Lazy<'a option>) =
        match m1 with
        | Some _ as m -> m
        | None -> m2.Force()
    
    let (+) = mplus
    
    let lookUp name = maybe {
        let! combined = fullNamesDb.TryFind name + lazy (nickNamesDb.TryFind name)
        return! prefsDb.TryFind combined
    }
    

提交回复
热议问题