-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture1A.rkt
47 lines (37 loc) · 981 Bytes
/
lecture1A.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
41
42
43
44
45
#lang racket
; Author: Zilu Tian
; Date: March 15, 2020
; Lecture 1A. Overview and Introduction to Lisp
;Methods of Managing Complexity
;- Black-box abstraction
;- Conventional Interfaces
;- Metalinguistic Abstraction
;
;Learning a new language
;- Primitives
;- Means of Composition
;- Means of Abstraction
(require "common.rkt")
; Heron's square-root method
;To find an approximation to \sqrt(x)
;- Make a guess G
;- Improve the guess by averaging G and x/G
;- Keep improving the guess until it is good enough
(define square-root
(λ (x)
(define improve
(λ (guess x)
(average guess (/ x guess))))
(define good-enough?
(λ (f guess value)
(let ([tolerance 0.001])
(if (< (abs (- (f guess) value)) tolerance)
true
false))))
(define try
(λ (guess)
(if (good-enough? square guess x)
guess
(try (improve guess x)))))
(try 1)))
(square-root 9)