Mozart Oz 中相同代码的函数和过程表现不同?
我尝试使用两种方法打印 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中具有类似的效果。
I try printing out the Fibonacci sequence in Oz using 2 approach : function and procedure using Emac as editor.
Procedure goes here :
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}
and function:
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}
The problem is the only Procedure "Loop" works. Result is:
1
1
2
3
5
8
13
21
34
55
Function "Loo" doesn't and it throws some hard-to-understand errors:
%********************** 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)
I still don't know why. As I think function and procedure has similar effect in OZ.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数必须使用函数调用语法来调用:
或者使用附加参数来接收值:
_
发音为“不关心”,意味着不需要变量的值。此外,函数必须通过将表达式作为每个分支的最后部分来返回一个值。因此,您的固定
Loo
函数将如下所示:但是,如果您没有任何有趣的返回内容,那么使用这样的循环函数就没有多大意义。也许您真正想要的是 构建一个列表并将其作为结果返回?
Functions must be called either with function call syntax:
or alternatively with an additional parameter to receive the value:
_
is pronounced "don't care" and means that the value of the variable is not needed.Also, functions must return a value by having an expression as the last part of every branch. So your fixed
Loo
function would look like this:However, using a function for looping like this does not make much sense if you don't have anything interesting to return. Maybe what you really want is to build a list and return it as the result?
第 13 行的
Loo
定义中有拼写错误。您正在调用
Loop
,但它不存在。我认为您应该致电Loo
。更新:您所看到的是由于函数和过程之间的差异;函数总是返回值,而过程则不然。您向
Loo
(K-1
) 提供单个参数,但Loo
需要两个参数;一个输入变量和一个捕获返回值的变量。 Oz 通过说您对Loo
应用了错误的元数来告诉您这一点(当您应该应用两个参数(二进制)时,您正在应用一个参数(一元))。这意味着您还必须将返回值分配给变量。执行以下操作之一:
A = {Loo K-1}
{Loo K-1 A}
A
是返回值将指向的变量被分配。对于不关心函数返回什么的情况,常见的约定是使用_
作为返回变量名称。You have a typo in the definition of
Loo
at line 13.You're calling
Loop
, which does not exist. I assume you should be callingLoo
.UPDATE: What you are seeing is due to the difference between functions and procedures; functions always return values, procedures don't. You are giving a single argument to
Loo
(K-1
), butLoo
needs two arguments; one input variable and one variable to capture the return value. Oz tells you this by saying that you're applying the wrong arity toLoo
(you're applying one argument (unary) when you should be applying two arguments (binary)).This means you have to assign the return value to a variable as well. Do one of:
A = {Loo K-1}
{Loo K-1 A}
A
is the variable to which the return value will be assigned. A common convention for cases where you don't care what the function returns is to use_
as the return variable name.