; folds
(define (foldr f a xs)
(if (null? xs) a
(f (car xs) (foldr f a (cdr xs)))))
(define (foldl f a xs)
(if (null? xs) a
(foldl f (f a (car xs)) (cdr xs))))
(define (foldr1 f xs)
(if (null? (cdr xs)) (car xs)
(f (car xs) (foldr1 f (cdr xs)))))
(define (foldl1 f xs)
(foldl f (car xs) (cdr xs)))
(define (scan f a xs)
(define (scanx f a xs)
(if (null? xs) xs
(scan f (f a (car xs)) (cdr xs))))
(cons a (scanx f a xs)))
(display (foldr + 0 '(1 2 3 4))) (newline)
(display (foldl + 0 '(1 2 3 4))) (newline)
(display (foldr cons '() '(1 2 3 4))) (newline)
(display (foldl cons '() '(1 2 3 4))) (newline)
(define (plusone _ n) (+ n 1))
(display (foldr plusone 0 '(1 2 3 4))) (newline)
(define (snoc xs x) (cons x xs))
(display (foldl snoc '() '(1 2 3 4))) (newline)
(display (foldr1 max '(1 2 3 4))) (newline)
(display (foldl1 max '(1 2 3 4))) (newline)
(display (scan + 0 '(1 2 3 4))) (newline)
(display (map reverse (scan snoc '() '(1 2 3 4)))) (newline)