簡體   English   中英

F# Continuation Passing Style (int -> int -> int) 選項

[英]F# Continuation Passing Style (int -> int -> int) option

我正在制作一個函數,它接受一個函數,2 個整數值作為輸入。 我必須檢查函數和值是 None 還是 Some。 Some, None 和 option 相當混亂。 這個概念很簡單,但我不知道如何將它應用到 F# 代碼中。

在問題中,輸入將是

第一個問題

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

  let fst = Some 42

  let snd = Some 42

expectation = Some 84

第二個問題

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

  let fst = None

  let snd = Some 42

expectation = None

我也必須使用

type MaybeBuilder () =

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

  member __.Return (m) = Some m

let maybe = MaybeBuilder ()

這個建設者。

我試過讓! 和許多其他瘋狂的事情,但失敗了。 一些 (fun xy -> x+y) 是 (int -> int -> int) 選項,我不知道如何操作這個函數。 它一直說這不是一個功能,它不能被應用。

像這樣的東西會起作用:

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

因為您的函數在 Maybe 中,所以您不能直接使用它。 使用let! 讓你到達可能的內部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM