Problem with Computing Workflow

trying to follow the example in the f # expert book and has problems with workflows ... the code looks like this:

type Attempt<'a> = option<'a>
let succeed x    = Some (x)
let fail         = None 

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

let delay f = f()

type AttemptBuilder() = 

    member b.Return (x) = succeed x
    member b.Bind (p, rest) = bind p rest
    member b.Delay (f) = delay f
    member b.Let (p, rest):Attempt<'a> = rest p  //'
    member b.ReturnFrom x = x


// using it: 
let attempt = new AttemptBuilder()

let test foo = 
    attempt {
        if not foo then return! fail else return foo
    }

let check () = 
    attempt {

        let! n1 = test true
        let! n2 = test false
        let! n3 = test true
        let foo = n1,n2,n3
        return foo
    }
let foo = check ()
Problem

is that when all values ​​are true, I get, as expected, Some (true, true, true), but if one of the values ​​passed to the value is false, foo is null (!). Any ftw?

thank!

+3
source share
1 answer

This is simply because Noneit actually appears as nullat runtime (see the notes on the page Option<'T>on MSDN .). Also note that you can add

member x.Zero() = fail

,

let test x = attempt { if x then return foo }

.

+3

Source: https://habr.com/ru/post/1775321/


All Articles