RosettaCodeData/Task/Catamorphism/Scheme/catamorphism.ss

8 lines
277 B
Scheme
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
(define (reduce fn init lst)
(do ((val init (fn (car rem) val)) ; accumulated value passed as second argument
(rem lst (cdr rem)))
((null? rem) val)))
(display (reduce + 0 '(1 2 3 4 5))) (newline) ; => 15
(display (reduce expt 2 '(3 4))) (newline) ; => 262144