Mozart Oz 中相同代码的函数和过程表现不同?

发布于 2024-11-04 05:27:00 字数 1390 浏览 7 评论 0原文

我尝试使用两种方法打印 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-11-11 05:27:00

函数必须使用函数调用语法来调用:

_ = {Loo 10}

或者使用附加参数来接收值:

{Loo 10 _}

_ 发音为“不关心”,意味着不需要变量的值。

此外,函数必须通过将表达式作为每个分支的最后部分来返回一个值。因此,您的固定 Loo 函数将如下所示:

fun {Loo L}
   if L == 1 then
      {Browse L}
      unit
   else
      _ = {Loo L-1}
      {Browse {Fibo L}}
      unit
   end
end
_ = {Loo 10}

但是,如果您没有任何有趣的返回内容,那么使用这样的循环函数就没有多大意义。也许您真正想要的是 构建一个列表并将其作为结果返回

Functions must be called either with function call syntax:

_ = {Loo 10}

or alternatively with an additional parameter to receive the value:

{Loo 10 _}

_ 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:

fun {Loo L}
   if L == 1 then
      {Browse L}
      unit
   else
      _ = {Loo L-1}
      {Browse {Fibo L}}
      unit
   end
end
_ = {Loo 10}

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?

乞讨 2024-11-11 05:27:00

第 13 行的 Loo 定义中有拼写错误。

您正在调用 Loop,但它不存在。我认为您应该致电 Loo

更新:您所看到的是由于函数和过程之间的差异;函数总是返回值,而过程则不然。您向 Loo (K-1) 提供单个参数,但 Loo 需要两个参数;一个输入变量和一个捕获返回值的变量。 Oz 通过说您对 Loo 应用了错误的元数来告诉您这一点(当您应该应用两个参数(二进制)时,您正在应用一个参数(一元))。

这意味着您还必须将返回值分配给变量。执行以下操作之一:

  1. A = {Loo K-1}
  2. {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 calling Loo.

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), but Loo 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 to Loo (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:

  1. A = {Loo K-1}
  2. {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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文