So, this is my second F# console application. The code goes like this:
let rec fact n =
if n = 1 then 1.0
else float(n)*fact(n-1);;
let rec pow x n =
if n = 1 then x
else float(x) * pow x (n-1);;
let rec func1 eps x n =
let f = fact (2 * n) * (fun n -> pow x n / float(n)) (2*n+1) / (pow 4.0 n * pow (fact n) 2)
if abs f < eps then f
else f + func1 eps x (n+1);;
let quarter = func1 1.0e-2 1.0 2;;
When I try to run let quarter = func1 1.0e-2 1.0 2;;
, interactive console says error FS0039: The value or constructor 'func1' is not defined
.
What did go wrong or what does my code lack?
Thanks in advance.
When you use F# interactive, you first need to evaluate the code that defines the functions that you want to use. So, if you select just the last line of your script, you get an error. I suppose you are using Alt+Enter to send the code to F# interactive - if you were copying the code and entering it by hand, you would see something like this:
Microsoft (R) F# Interactive version 12.0.30815.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> let quarter = func1 1.0e-2 1.0 2;;
let quarter = func1 1.0e-2 1.0 2;;
---------------^^^^^
stdin(1,16): error FS0039: The value or constructor 'func1' is not defined
To fix things, you need to select everything in your file (except for the last line) and hit Alt+Enter. Then you should see something like:
>
val fact : n:int -> float
>
val pow : x:float -> n:int -> float
>
val func1 : eps:float -> x:float -> n:int -> float
This tells you that the F# Interactive processed the function definitions fact
, pow
and func1
and you can now use them in other expressions that you're evaluating:
> let quarter = func1 1.0e-2 1.0 2;;
val quarter : float = 0.2250279793
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With