Given a sequence of sequences, applies accumulate to the first item from each, then the next item of each, etc.
Subproblem: define a proc that returns the first item from each nested sequence, and another that returns the remaining parts (accumulate-n will accumulate the former, and call itself with the latter):
(define(select-cars sequence)(mapcar sequence))(define(select-cdrs sequence)(mapcdr sequence));; Test
(define t (list(list 1 2 3)(list 40 50 60)(list 700 800 900)))(select-cars t)(select-cdrs t);; Accumulates the result of the first and the already-accumulated
;; rest.
(define(accumulate op initial sequence)(if(null? sequence)
initial
(op (car sequence)(accumulate op initial (cdr sequence)))));; accumulate-n
(define(accumulate-n op init sequence)(define nil '())(if(null?(car sequence))
nil
(cons(accumulate op init (mapcar sequence))(accumulate-n op init (mapcdr sequence)))));; Usage:
(accumulate-n + 0 t)
Originally I had (map (lambda (s) (car s)) sequence), but (lambda (s) (car s)) is just a function that returns car ...
Accumulate-n.
Given a sequence of sequences, applies accumulate to the first item from each, then the next item of each, etc.
Subproblem: define a proc that returns the first item from each nested sequence, and another that returns the remaining parts (accumulate-n will accumulate the former, and call itself with the latter):
Originally I had (map (lambda (s) (car s)) sequence), but (lambda (s) (car s)) is just a function that returns car ...