0
fun isfib(a) =
   findfib(a,1,1)

and
findfib(b,x,y) =
   val z = x + y
   if b <= 1 then true
   else if z > b then false
   else if z = b then true
   else fib(b,y,z)

我在程序中输入了一个输入,并递归地尝试找出这个输入是否是一个 fib 数字。我可以在 2 行的单独程序中计算 fib 的第 x 个位置。但是这种“输入,检查是否等于 fib,将 fib 设为无穷大或破产”的逻辑让我非常困惑。我还在第 7.1 行遇到错误,例如“用 ANDALSO 替换 AND”和“插入 ORELSE”,即 B<=1

4

1 回答 1

0

I mean that you should use and for cases where you need mutual recursion, in this case it doesn't seems like you really do, for example, you could try something like this:

fun fib 0 = 1
    | fib 1 = 1
    | fib n = (fib (n-1)) + (fib (n-2));

fun isfib a = 
    let
        fun eval a b = if a = (fib b) then true else if a < (fib b) then false else eval a (b+1)
    in
        eval a (1)
end;

Try running isfib 5 and isfib 6 for example. No infinity required.

于 2013-02-14T01:05:06.530 回答