procedure foldr(f, accumulated, list) if *list = 0 then return accumulated else return f(list[1], foldr(f, accumulated, list[2:0])) end procedure foldl(f, accumulated, list) if *list = 0 then return accumulated else return foldl(f, f(accumulated, list[1]), list[2:0]) end procedure add(a, b) return a + b end procedure concat(value, accumulated) return value || accumulated end procedure eo_helper(thusFar, elem) if elem % 2 == 0 then return [thusFar[1]+1, thusFar[2]] else return [thusFar[1], thusFar[2]+1] end procedure paired(s) return foldl(paired_helper, 0, s) == 0 end procedure paired_helper(count, c) if count = -1 then return -1 case c of { "(": return count + 1 ")": return count - 1 default: return count } end procedure show(s) band := repl("-",5) || " " write("\n", band, s, reverse(band)) end procedure main() &trace := -1 show("foldr(add, 0, [1,2,3,4,5])") foldr(add, 0, [1,2,3,4,5]) show("foldl(add, 0, [1,2,3,4,5])") foldl(add, 0, [1,2,3,4,5]) show("foldr(concat, \"\", [1,2,3,4,5])") foldr(concat, "", [1,2,3,4,5]) show("foldl(\"||\", \"\", [1,2,3,4,5])") foldl("||", "", [1,2,3,4,5]) show("paired(\"(())()\")") paired("(())()") show("paired(\"(()\")") paired("(()") show("eo_helper, [0,0], [1,3,5,6,7])") foldl(eo_helper, [0,0], [1,3,5,6,7]) end