[英]F# Order of Execution using CPS/Partial Application
為什么要先打印“ DIV / 0”,然后再打印“ 2”?
let printZero = printfn "DIV/0"
let printSuccess x = printfn "%d" x
let div ifZero success x y =
if y = 0
then ifZero
else x / y |> success
let printDiv = div printZero printSuccess
printDiv 8 4
printDiv 10 0
printfn "DIV/0"
將立即寫入控制台,返回unit
。 所以這行:
let printZero = printfn "DIV/0"
...將立即寫入DIV/0
並將unit
綁定到值printZero
。 稍后,當您使用y = 0
調用div
函數時,只需返回該值。
您想要的是printZero
是一個功能。 由於該函數不需要任何值作為輸入,因此您也可以在此處使用unit
(表示為()
)-因此您具有類型為unit -> unit
的函數:
let printZero() = printfn "DIV/0"
let printSuccess x = printfn "%d" x
let div ifZero success x y =
if y = 0
then ifZero()
else x / y |> success
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.