; the first computer program
(define (but-last xs)
(let loop ((xs xs) (zs '()))
(if (null? (cdr xs)) (reverse zs)
(loop (cdr xs) (cons (car xs) zs)))))
(define (bernoulli limit)
(let loop ((n 1) (bs '(-1/2)) (ns '()) (d 1) (ds '()))
;(display "loop ") (display n) (display " ") (display bs)
;(display " ") (display ns) (display " ") (display ds) (newline)
(if (= limit n) bs
(let* ((2n (* n 2)) (2n-1 (- 2n 1)) (2n+1 (+ 2n 1))
(ns (cons 2n (map (lambda (x) (* 2n 2n-1 x)) ns)))
(b (- (apply + (map * bs (cons 2n-1 (but-last ns)) (map / (cons 2n+1 ds))))))
(bs (append bs (list b)))
(d (* d 2n 2n-1))
(ds (append ds (list d))))
(loop (+ n 1) bs ns d ds)))))
(display (bernoulli 10))