[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/rgXtNfmM    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Feb 7:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; 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))


Output:
1
(-1/2 1/6 -1/30 1/42 -1/30 5/66 -691/2730 7/6 -3617/510 43867/798)


Create a new paste based on this one


Comments: