Function and procedure behave differently with the same code in Mozart Oz?

I am trying to print a Fibonacci sequence in Oz using 2 approaches: a function and a procedure, using Emac as an editor. The 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 "Loop" procedure. Result:

 1 1 2 3 5 8 13 21 34 55 

The "Loo" function does not and 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. In my opinion, function and procedure have a similar effect in OZ.

+7
source share
2 answers

Functions must be called either with the function call syntax:

 _ = {Loo 10} 

or, alternatively, with an additional parameter to get the value:

 {Loo 10 _} 

_ pronounced "don't care" and means that the value of the variable is not required.

In addition, functions must return a value, having an expression as the last part of each branch. So your fixed Loo function will 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 cyclization like this does not make much sense if you have nothing to return. Is it possible that you really want to create a list and return it as a result ?

+6
source

You have a typo in the definition of Loo on line 13.

You call a Loop that does not exist. I assume you should call Loo .

UPDATE: What you see is related to the difference between functions and procedures; functions always return values, procedures do not. You specify one 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 apply the wrong arity to Loo (you apply one argument (unary) when you must apply two arguments (binary)).

This means that you must also assign the return value to the variable. Do one of:

  • A = {Loo K-1}
  • {Loo K-1 A}

A is the variable to which the return value will be assigned. The general convention for cases where you don't care what the function returns is to use _ as the name of the returned variable.

+3
source

All Articles