Skip to content

Commit a89101b

Browse files
committed
up to exercise 1.8
1 parent 8ce536f commit a89101b

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed

kenric/ex.1.1.rkt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#lang racket
2+
3+
10 ;10
4+
(+ 5 3 4) ;12
5+
(- 9 1) ;8
6+
(/ 6 2) ;3
7+
(+ (* 2 4) (- 4 6)) ;6
8+
(define a 3) ;
9+
(define b (+ a 1)) ;
10+
(+ a b (* a b)) ;19
11+
(= a b) ;#f
12+
(if (and (> b a) (< b (* a b)))
13+
b
14+
a) ;4
15+
16+
(cond ((= a 4) 6)
17+
((= b 4) (+ 6 7 a))
18+
(else 25)) ;16
19+
20+
(+ 2 (if (> b a) b a)) ;6
21+
22+
(* (cond ((> a b) a)
23+
((< a b) b)
24+
(else -1))
25+
(+ a 1)) ;16

kenric/ex.1.2.rkt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#lang racket
2+
3+
(/ (+ 5 4 (- 2
4+
(- 3 (+ 6 (/ 4 5)))))
5+
(* 3 (- 6 2) (- 2 7)))

kenric/ex.1.3.rkt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#lang racket
2+
3+
(define (f n1 n2 n3)
4+
(cond [(and (< n1 n2) (< n1 n3))
5+
(+ (* n2 n2) (* n3 n3))]
6+
[(and (< n2 n1) (< n2 n3))
7+
(+ (* n1 n1) (* n3 n3))]
8+
[else (+ (* n1 n1) (* n2 n2))]))

kenric/ex.1.4.rkt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#lang racket
2+
3+
(define (a-plus-abs-b a b)
4+
((if (> b 0) + -) a b))

kenric/ex.1.5.rkt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#lang racket
2+
3+
(define (p) (p))
4+
(define (test x y)
5+
(if (= x 0) 0 y))
6+
7+
(test 0 (p))
8+
9+
; applicative order
10+
; will be infinite loop, because p will be evaluated first
11+
12+
; normal order
13+
; will evaluate to 0, because p will be passed in without evaluation, using the substitution model

kenric/ex.1.7.rkt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#lang racket
2+
3+
(define (square x)
4+
(* x x))
5+
6+
(define (improve guess x)
7+
(average guess (/ x guess)))
8+
9+
(define (average x y)
10+
(/ (+ x y) 2))
11+
12+
(define (good-enough? guess x)
13+
(< (abs (- (square guess) x)) (* x 0.001)))
14+
15+
(define (sqrt-iter guess x)
16+
(if (good-enough? guess x)
17+
guess
18+
(sqrt-iter (improve guess x) x)))
19+
20+
; For very small numbers, the difference between the square of the guess compared
21+
; to the radicand would easily be under the threshold of 0.001. This is because
22+
; square of numbers less than one would yield a smaller output compared to input
23+
24+
; For very large numbers, a large number of iterations needed to get below the hard coded
25+
; threshold, and machine doesn't have enough precision to work out small differences
26+
; between large numbers

kenric/ex.1.8.rkt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#lang racket
2+
3+
(define (cube x)
4+
(* x x x))
5+
6+
(define (improve guess x)
7+
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
8+
9+
(define (good-enough? guess x)
10+
(< (abs (- (cube guess) x)) (* x 0.001)))
11+
12+
(define (cube-iter guess x)
13+
(if (good-enough? guess x)
14+
guess
15+
(cube-iter (improve guess x) x)))

0 commit comments

Comments
 (0)