Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value or constructor is not defined

Tags:

f#

fibonacci

I'm learning f# and I've got a pretty trivial problem that doesn't seem to make sense. I'm working on Project Euler problem 2 and I've got this:

let fib (x : BigInteger) (y : BigInteger) (max : BigInteger) = 
    let added = x + y
    if added > max then y
    else fib y (x + y) max

I've got the error at the recursive fib call:

Value or constructor 'fib' is not defined

And I'm not sure why. Any help?

like image 898
Steven Evers Avatar asked Mar 10 '12 01:03

Steven Evers


2 Answers

Because fib is recursive function, it has to start with let rec.

like image 159
pad Avatar answered Nov 03 '22 07:11

pad


In F#, if you want to write a recursive function, you have to use the rec keyword:

let rec fib (x : BigInteger) (y : BigInteger) (max : BigInteger) = 
    let added = x + y
    if added > max then y
    else fib y (x + y) max

That's because in F# under normal circumstances, you can only use identifiers declared before the current code, unlike in C#.

like image 7
svick Avatar answered Nov 03 '22 07:11

svick