-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-31.rkt
40 lines (30 loc) · 858 Bytes
/
1-31.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#lang racket
(define (assert-eql actual expected)
(if (not (= expected actual))
(error "Does not match condition, expected: "
expected " actual: " actual)
true))
(define (inc n) (+ n 1))
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))
(define (factorial n)
(product identity 1 inc n))
(assert-eql (factorial 5) 120)
(define (product-recur term a next b)
(if (> a b)
1
(* (term a)
(product-recur term (next a) next b))))
(define (factorial-recur n)
(product-recur identity 1 inc n))
(assert-eql (factorial-recur 5) 120)
(define (pi-approx n)
(define (term n)
(cond [(even? n) (/ (+ n 2) (+ n 1))]
[else (/ (+ n 1) (+ n 2))]))
(* 4.0 (product term 1 inc n)))
(pi-approx 10000)