【发布时间】:2009-01-07 13:27:21
【问题描述】:
我正在尝试在 F# 中获得此 vb.net 代码的完全等效(非功能性):
Function FastPow(ByVal num As Double, ByVal exp As Integer) As Double
Dim res As Double = 1
If exp < 1 Then
If exp = 0 Then Return res
exp = -exp
num = 1 / num
End If
Do While exp > 1
If exp Mod 2 = 1 Then
res = res * num
num = num * num
exp = exp >> 1
Loop
Return res * num
End Function
这是我写的:
let FastPow num exp =
let mutable ex = exp
let mutable res = 1
let mutable n = num
if ex < 1 then
if ex = 0 then res
ex <- -ex
n <- 1 / n
while ex > 1 do
if (ex % 2 = 1) then
res <- res * n
n <- n * n
exp >>> 1
res * n
但是在 res 的“if ex = 0 then res”行中我收到了一个错误:
“此表达式具有 int 类型,但在这里与类型 unit 一起使用”。
我不明白为什么它会给我这个错误。
编辑:我实际上也收到了警告:
“这个表达式应该有 'unit' 类型,但有 'int' 类型。”
在“如果(例如 % 2 = 1)那么”
【问题讨论】: