Search code examples
.netf#pattern-matchingmonadscomputation-expression

Making computation expressions using single case discriminated unions


If I have a function that attempts to divide a starting number two times. The entire workflow has to return a boolean.

let divideBy bottom top =
    if bottom = 0 then None
    else Some (top/bottom)

let divideByWorkflow init x y = 
    match init |> divideBy x with
    | None -> false
    | Some a -> 
        match a |> divideBy y with
        | None -> false
        | Some b -> true

let good = divideByWorkflow 12 3 2
let bad = divideByWorkflow 12 3 0

Is the following builder correct?

type BoolMaybe = BoolMaybe with
    member __.Bind (expr, f) =
        match expr with
        | Some value -> f value
        | None -> false
    member __.Return expr = expr

let divideByWorkflow init x y =
    BoolMaybe {
        let! a = init |> divideBy x
        let! b = a |> divideBy y
        return true
    }

Solution

  • I concur with Dzoukr's answer, mine looks just slightly different:

    let divideBy bot top =
        match bot with
        | 0 -> None
        | _ -> Some (top / bot)
    
    let divideByWorkflow init x y =
        Some init
        |> Option.bind (divBy x)
        |> Option.bind (divBy y)
        |> Option.isSome
    

    No computation expression required, it does not seem to add any benefits here.