so, does this mean (lambda (x y) (cons (p x) y)) is the 'op' part?
yes.
& if so, what are x & y & how are they passed into the equation?
x
is "the current element" and y
is "the result of recursively calling (accumulate ...)
on the rest of the list".
Here's what this means. Each given list is seen as a cons pair - a pair of its car
and its cdr
. The value of a list's car
goes into x
, and of cdr
- to the recursive call whose result goes into y
. Or in equational style,
accumulate( op, z, CONS(e,es) ) = op( e, accumulate(op, z, es) )
where CONS(e,es)
is not a function call, but a data representation (using the upper case to signify this) - a cons cell with e
in its car
and es
(read: eez, like in e, plural) in its cdr
. So when op(x,y) = ...
is called, x = e
and y = accumulate(op, z, es)
are passed into it.
The above equation defines the function accumulate
. One more equation is needed, for the NIL
case:
accumulate( op, z, NIL ) = z
Thus it is assumed that op
is a binary operation (i.e. receiving two arguments), able to deal with results of accumulate
as its second argument. This pattern of list processing is known as "folding", or "catamorphism" - i.e. processing the data down, analyzing the data into its constituent parts, and recombining them in a certain fashion, arranging for a certain "call protocol" for the binary operation.
There are other patterns as well, e.g. a so-called "paramorphism",
accumulate2( op, z, NIL ) = z
accumulate2( op, z, CONS(e,es) ) = op( e, es, accumulate2(op, z, es) )
Here the operation is assumed to be ternary (i.e. receiving three arguments), receiving the current element, the rest of input list, and the result of recursive processing of the rest of the input list. It is useful in implementing patterns of data processing which need to have access to input as well as output (what's sometimes referred to, cryptically, as "having your cake and eating it too").
To be more useful these definitions need to be encoded lazily, so op
has a choice of whether to force the recursive result or not, to be able to abort early (see e.g. this).