未定义值或构造函数

Ste*_*ers 8 f# fibonacci

我正在学习f#而且我有一个非常微不足道的问题似乎没有意义.我正在研究Project Euler问题2,我得到了这个:

let fib (x : BigInteger) (y : BigInteger) (max : BigInteger) = 
    let added = x + y
    if added > max then y
    else fib y (x + y) max
Run Code Online (Sandbox Code Playgroud)

我在递归的fib调用时遇到错误:

值或构造函数'fib'未定义

而且我不确定为什么.有帮助吗?

pad*_*pad 13

因为fib是递归函数,所以必须从头开始let rec.


svi*_*ick 7

在F#,如果你想写一个递归函数,你必须使用rec关键字:

let rec fib (x : BigInteger) (y : BigInteger) (max : BigInteger) = 
    let added = x + y
    if added > max then y
    else fib y (x + y) max
Run Code Online (Sandbox Code Playgroud)

这是因为在正常情况下,在F#中,您只能使用在当前代码之前声明的标识符,这与C#不同.