OZ Programming Language This exercise investigates how to use cells together wit
ID: 3888215 • Letter: O
Question
OZ Programming Language
This exercise investigates how to use cells together with functions. Let us define a function Accumulate N that accumulates all its inputs, i.e., it adds together all the arguments of all calls. Here is an example Browse lAccumulate 5) Browse Accumulate 100)1 Brows Accumulate 45 This should display 5, 105, and 150, assuming that the accumulator contains zero at the start Here is a wrong way to write Accumulate declare fun Accumulate N Acc in Acc end What is wrong with this definition? How would you correct it?Explanation / Answer
In the given program, as Acc is declared inside function, for every function call it is getting initialized with new value. So, value in Acc variable is not stored till the second function call.
It is mandatory to declare Acc variable before function call for the given recruitment.
Ex
declare Acc={NewCell 0}
fun {Accumulate N}
Acc:=@Acc+N
@Acc
end
{Browse {Accumulate 5}} {Browse {Accumulate 100}} {Browse {Accumulate 45}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.