(define(fold-right op initial sequence)(if(null? sequence)
initial
(op (car sequence)(accumulate op initial (cdr sequence)))))(define(fold-left op initial sequence)(define(iter result rest)(if(null? rest)
result
(iter (op result (car rest))(cdr rest))))(iter initial sequence))
For fold-right: use append to ensure the list is built with correct nesting, and put the first item in a list so it can be appended.
For fold-left: The inner iter in fold-left keeps peeling off the first item and doing something with it and the current result, then passing that result and the remaining items to itself. So, items are getting handled in order, from left to right, and just need to be added to the result.
For fold-right: use append to ensure the list is built with correct nesting, and put the first item in a list so it can be appended.
For fold-left: The inner iter in fold-left keeps peeling off the first item and doing something with it and the current result, then passing that result and the remaining items to itself. So, items are getting handled in order, from left to right, and just need to be added to the result.