我是 f# 新手,正在撰寫一個程式來求解 xuvat 方程,但是編譯器無法識別我的函式,并且出現錯誤 The value or constructor 'xuvat' is not defined.
這是我的功能:
let xuvat xuvatTuple =
match xuvatTuple with
| (x, u, v, a, t) ->
match ((x<>null), (u<>null), (v<>null), (a<>null), (t<>null)) with
| (true, true, true, _, _) -> (x, u, v, ((v**2 - u**2)/(2*x)), ((2*x)/(u v)))
| (true, true, _, true, _) -> (x, u, (u a*t), a, ((sqrt (u**2 (2 * a * x)) - u) / a))
| (true, _, true, true, _) -> (x, (v - (a*t)), v, a, ((v - sqrt (v**2 - (2 * a * x))) / a))
| (_, true, true, true, _) -> (((u**2 v**2)/2*a), u, v, a, ((v-u)/a))
| (_, true, true, _, true) -> (((u v)/2), u, v, ((v-u)/t), t)
| (_, true, _, true, true) -> ((u*t (a*t*t)/2), u, (u a*t), a, t)
| (_, _, true, true, true) -> ((v*t - (a*t*t)/2), (v - a*t), v, a, t)
| (true, _, _, true, true) -> (x, ((x - a*t*t)/2*t), ((x a*t*t)/2*t), a, t)
| (true, _, true, _, true) -> (x, (((2*x)/t) v), v, ((2*v*t-2*x)/t**2), t)
//| (true, true, _, _, true) -> (x, u, (((2*x)/t) - u), ((2*x -2*u*t)/t**2), t)
| _ -> failwith "NOT ENOUGH INFORMATION"
這是我的代碼的入口點以及我的函式被呼叫的地方:
[<EntryPoint>]
let main argv =
let xuvatTuple = (7, 0, null, null, 4)
let finalTuple = xuvat xuvatTuple
printfn $"{finalTuple}"
請你能幫我找到答案。
uj5u.com熱心網友回復:
主要問題不是xuvat無法識別該函式,而是xuvat具有型別錯誤的事實,因此編譯器不接受它(因此后來未定義)。
當我將您的代碼復制到 Visual Studio 中時,它實際上并沒有顯示錯誤(在 IntelliSense 中),而是僅在我嘗試編譯它時才顯示,這令人困惑。
問題是您正在接受一些引數(作為元組)并使用它們進行數值運算,但也將它們與null. 但是數字型別不允許null值,所以這行不通。同樣,您的xuvatTuple包含一些int值和一些obj值,這也可能不是您想要的。
您可以通過使用option值來解決此問題:
let xuvat xuvatTuple =
match xuvatTuple with
| (Some x, Some u, Some v, _, _) ->
(x, u, v, ((v*v - u*u)/(2*x)), ((2*x)/(u v)))
let xuvatTuple = (Some 7, Some 0, None, None, Some 4)
let finalTuple = xuvat xuvatTuple
我只做過第一個案例,但你明白了。我覺得也有在你的第二個案例中的錯誤,因為你的計算是指t,但推測這是null(你立即看到,如果你使用這樣的模式匹配!)我也改u**2到u*u,由于**對整數沒有定義。
uj5u.com熱心網友回復:
我已經復制了你所看到的。我認為問題在于xuvat包含許多其他編譯器錯誤,這會阻止編譯器推斷其回傳型別,從而阻止編譯器識別您嘗試呼叫xuvat.
您可以通過顯式宣告xuvat的回傳型別來公開這些其他錯誤,如下所示:
let xuvat xuvatTuple : (int * int * int * int * int) =
...
執行此操作后,您可以看到列出的錯誤:
Program.fs(5,40): error FS0001: The type 'int' does not have 'null' as a proper value. See also Program.fs(4,19)-(4,23).
Program.fs(5,43): error FS0001: The type 'int' does not have 'null' as a proper value. See also Program.fs(4,30)-(4,34).
Program.fs(5,46): error FS0001: The type 'int' does not have 'null' as a proper value. See also Program.fs(4,41)-(4,45).
Program.fs(5,51): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(5,58): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(6,57): error FS0001: The type 'int' does not have 'null' as a proper value. See also Program.fs(4,52)-(4,56).
Program.fs(6,68): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(6,84): error FS0001: The type 'int' does not support the operator 'Sqrt'
Program.fs(7,74): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(7,90): error FS0001: The type 'int' does not support the operator 'Sqrt'
Program.fs(8,42): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(8,49): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(9,68): error FS0001: The type 'int' does not have 'null' as a proper value. See also Program.fs(4,63)-(4,67).
Program.fs(13,76): error FS0001: The type 'int' does not support the operator 'Pow'
Program.fs(22,28): error FS0001: The type 'int' does not have 'null' as a proper value
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368963.html
