简体   繁体   English

Function 未被识别 f#

[英]Function is not being recognised f#

I am new to f# and was writing a program to solve xuvat equations, however my function was not being recognised by the compiler and I got an error The value or constructor 'xuvat' is not defined.我是 f# 的新手,正在编写一个程序来求解 xuvat 方程,但是我的 function 没有被编译器识别,我得到一个错误The value or constructor 'xuvat' is not defined.

This is my function:这是我的 function:

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"

This is the entry point for my code and where my function is being called:这是我的代码的入口点,我的 function 被调用:

[<EntryPoint>]
let main argv =

    let xuvatTuple = (7, 0, null, null, 4)
    
    let finalTuple = xuvat xuvatTuple
    printfn $"{finalTuple}" 

Please can you help me to to find the answer.请你能帮我找到答案。

The primary issue is not that the xuvat function is not recognized, but the fact that xuvat has a type error and so the compiler does not accept it (and so it is later not defined).主要问题不是无法识别xuvat function,而是xuvat存在类型错误,因此编译器不接受它(因此后来没有定义)。

When I copy your code into Visual Studio, it does not actually show the error (in IntelliSense), but only when I try to compile it, which is confusing.当我将您的代码复制到 Visual Studio 中时,它实际上并没有显示错误(在 IntelliSense 中),但只有在我尝试编译它时才会显示,这令人困惑。

The issue is that you are taking some arguments (as a tuple) and you do numerical operations with them, but also compare them with null .问题是您正在使用一些 arguments (作为元组)并且您对它们进行数值运算,但还将它们与null进行比较。 But numerical types do not allow null values, so this cannot work.但是数字类型不允许null值,所以这不起作用。 Similarly, your xuvatTuple contains some int values and some obj values, which is also probably not what you wanted.同样,您的xuvatTuple包含一些int值和一些obj值,这也可能不是您想要的。

You could fix this by using option values instead:您可以改用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

I've only done the first case, but you get the idea.我只做了第一个案例,但你明白了。 I think there is also a bug in your second case, because your computation refers to t , but this is supposedly null (which you see immediately if you use pattern matching like this!) I also changed u**2 to u*u , because ** is not defined on integers.我认为在你的第二种情况下也有一个错误,因为你的计算是指t ,但这应该是null (如果你使用这样的模式匹配,你会立即看到!)我还将u**2更改为u*u ,因为**没有在整数上定义。

I've reproduced what you're seeing.我已经复制了你所看到的。 I think the problem is that xuvat contains numerous other compiler errors, which prevents the compiler from inferring its return type, which in turn prevents the compiler from recognizing your attempt to call xuvat .我认为问题在于xuvat包含许多其他编译器错误,这会阻止编译器推断其返回类型,从而阻止编译器识别您调用xuvat的尝试。

You can expose these other errors by explicitly declaring xuvat 's return type, like this:您可以通过显式声明xuvat的返回类型来公开这些其他错误,如下所示:

let xuvat xuvatTuple : (int * int * int * int * int) =
   ...

Once you do that, you can see the errors listed:一旦你这样做,你可以看到列出的错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM