(wat-aro)

無職から有職者にランクアップしました

SICP 問題 2.38

(define (fold-left op initial sequence)
  (define (iter result rest)
    (if (null? rest)
        result
        (iter (op result (car rest))
              (cdr rest))))
  (iter initial sequence))

(define (fold-right op initial sequence)
  (if (null? sequence)
      initial
      (op (car sequence)
          (fold-right op initial (cdr sequence)))))
gosh> (fold-right / 1 (list 1 2 3))
3/2
gosh> (fold-left / 1 (list 1 2 3))
1/6
gosh> (fold-right list nil (list 1 2 3))
(1 (2 (3 ())))
gosh> (fold-left list nil (list 1 2 3))
(((() 1) 2) 3)

fold-rightとfold-leftによってopが満たすべき条件は+や×のように順番に影響されない手続きであること.