(define (product factor a next b)
  (define (iter pos acc)
    (if (> pos b)
        acc
        (iter (next pos) (* acc (factor pos)))))
  (iter a 1))
(define (factorial n)
  (product (lambda (x) x) 1 (lambda (x) (+ x 1)) n))