我尝试使用 2 种方法在 Oz 中打印出斐波那契数列:使用 Emac 作为编辑器的函数和过程。程序在这里:
declare
fun {Fibo N}
case N of
1 then 1
[] 2 then 1
[] M then {Fibo (M-1)} + {Fibo (M-2)}
end
end
declare
proc {Loop K}
if K ==1 then {Browse K}
else
{Loop K-1}
{Browse {Fibo K}}
end
end
{Loop 10}
和功能:
declare
fun {Fibo N}
case N of
1 then 1
[] 2 then 1
[] M then {Fibo (M-1)} + {Fibo (M-2)}
end
end
declare
fun {Loo L}
if L ==1 then {Browse L}
else
{Loo L-1}
{Browse {Fibo L}}
end
end
{Loo 10}
问题是唯一的程序“循环”有效。结果是:
1
1
2
3
5
8
13
21
34
55
函数“Loo”没有,它会抛出一些难以理解的错误:
%********************** static analysis error *******************
%**
%** illegal arity in application
%**
%** Arity found: 1
%** Expected: 2
%** Application (names): {Loo _}
%** Application (values): {<P/2> _<optimized>}
%** in file "Oz", line 13, column 6
%********************** static analysis error *******************
%**
%** illegal arity in application
%**
%** Arity found: 1
%** Expected: 2
%** Application (names): {Loo _}
%** Application (values): {<P/2> 10}
%** in file "Oz", line 17, column 0
%** ------------------ rejected (2 errors)
我仍然不知道为什么。我认为函数和过程在 OZ 中具有类似的效果。