Home > Blockchain >  Function is not being recognised f#
Function is not being recognised f#

Time:11-29

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.

This is my 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:

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

CodePudding user response:

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).

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.

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. But numerical types do not allow null values, so this cannot work. Similarly, your xuvatTuple contains some int values and some obj values, which is also probably not what you wanted.

You could fix this by using option values instead:

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.

CodePudding user response:

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.

You can expose these other errors by explicitly declaring xuvat's return type, like this:

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
  • Related