[英]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.