F# Continuation Passing Style (int -> int -> int) option

95 Views Asked by At

I am making a function that takes one function, 2 integer values as input. I have to check whether the function and the values are None or Some. Some, None and option is quite confusing. The concept is simple but I don't get how to apply it into F# codes.

In the problem, the input will be

First problem

  let f = Some (fun x y -> x + y)

  let fst = Some 42

  let snd = Some 42

expectation = Some 84

Second problem

  let f = Some (fun x y -> x + y)

  let fst = None

  let snd = Some 42

expectation = None

I also have to use

type MaybeBuilder () =

  member __.Bind (m, f) = Option.bind f m

  member __.Return (m) = Some m

let maybe = MaybeBuilder ()

this builder.

I tried let! and many other crazy things but failed. Some (fun x y -> x+y) is (int -> int -> int) option and I don't know how to manipulate this function. It keeps saying that this is not a function and it cannot be applied.

1

There are 1 best solutions below

1
On BEST ANSWER

Something like this would work:

let expectation =
    maybe {
        let! func = f
        let! a = fst
        let! b = snd
        return func a b
    }

Because your function is inside a Maybe, you cannot use it directly. Using let! allows you reach inside the maybe.