Function and procedure behave differently with the same code in Mozart Oz? -
i try printing out fibonacci sequence in oz using 2 approach : function , procedure using emac editor. procedure goes here :
declare fun {fibo n} case n of 1 1 [] 2 1 [] m {fibo (m-1)} + {fibo (m-2)} end end declare proc {loop k} if k ==1 {browse k} else {loop k-1} {browse {fibo k}} end end {loop 10}
and function:
declare fun {fibo n} case n of 1 1 [] 2 1 [] m {fibo (m-1)} + {fibo (m-2)} end end declare fun {loo l} if l ==1 {browse l} else {loo l-1} {browse {fibo l}} end end {loo 10}
the problem procedure "loop" works. result is:
1 1 2 3 5 8 13 21 34 55
function "loo" doesn't , throws 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. think function , procedure has similar effect in oz.
functions must called either function call syntax:
_ = {loo 10}
or alternatively additional parameter receive value:
{loo 10 _}
_
pronounced "don't care" , means value of variable not needed.
also, functions must return value having expression last part of every branch. fixed loo
function this:
fun {loo l} if l == 1 {browse l} unit else _ = {loo l-1} {browse {fibo l}} unit end end _ = {loo 10}
however, using function looping not make sense if don't have interesting return. maybe want build list , return result?
Comments
Post a Comment