Suppose you want to write a simple math reducer that will, for example, do the transformation:
((add ((add 2) 3)) ((add 4) 2)) -> 11
A simple algorithm could be (example in JavaScript):
function r(a){
if (typeof(a)!=="object") return a;
var a = [r(a[0]), r(a[1])];
return a[0][0]==="add" ? a[0][1] + a[1] : a;
}
console.assert(r([["add",[["add",2],3]],[["add",4],2]]) === 11);
Is it possible to solve the same problem with constant stack?