为什么这个函数说"只有简单的变量模式可以绑定'let rec'结构"?

Kal*_*exx 1 f#

我刚刚开始使用F#并且正在尝试问题Euler问题#3.为了找到素数,我想出了以下代码来计算所有素数到最大数:

let rec allPrimes foundPrimes, current, max =
    // make sure the current number isn't too high
    //   or the current number isn't divisible by any known primes
    if current >= max then
        foundPrimes
    else
        let nextValue = current + 1

        if not List.exists (fun x -> current % x = 0L) foundPrimes then
            allPrimes foundPrimes nextValue max
        else
            allPrimes (foundPrimes :: current) nextValue max 
Run Code Online (Sandbox Code Playgroud)

不幸的是,这给出了错误:

只有简单的变量模式才能绑定在'let rec'结构中

为什么我收到此错误?

Joh*_*mer 5

你不想把逗号放在声明中 - 改变

let rec allPrimes foundPrimes, current, max =
Run Code Online (Sandbox Code Playgroud)

let rec allPrimes foundPrimes current max =
Run Code Online (Sandbox Code Playgroud)

原件的正确版本将是

let rec allPrimes (foundPrimes, current, max) =
Run Code Online (Sandbox Code Playgroud)

注意元组周围的括号.但是,这需要修改递归调用以使用元组形式.在原始版本中,编译器认为您正在尝试执行类似的操作

let a,b,c=1,2,3
Run Code Online (Sandbox Code Playgroud)

这不适用于递归函数.