;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname heron) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; Number Number -> Number
;; guess is the current guess
(define better-guess
(lambda (x guess)
(/ (+ guess (/ x guess)) 2)))
;; Number Number -> Number
(define distance
(λ(a b)
(abs (- a b))))
(define epsilon 1e-100)
;; Number Number -> Boolean
(define good-enough?
(λ(x guess)
(< (distance guess (/ x guess)) epsilon)))
;; Number Number -> Number
;; x : approximate the square root of x; x > 0
; guess: initial guess
(define sqrt-iter
(λ(x guess)
(if (good-enough? x guess)
guess
(sqrt-iter x (better-guess x guess)))))
;; Number -> Number
(define my-sqrt
(λ(x)
(sqrt-iter x 1)))
;(define a 25) ;; approximate the square root of x
;(define guess-1 1) ;; initial guess
;(define guess-2 (better-guess a guess-1))
;(define guess-3 (better-guess a guess-2))
;(define guess-4 (better-guess a guess-3))
;(define guess-5 (better-guess a guess-4))
;(define guess-6 (better-guess a guess-5))
;(define guess-7 (better-guess a guess-6))
;(define guess-8 (better-guess a guess-7))
;(define guess-9 (better-guess a guess-8))
;(define guess-10 (better-guess a guess-9))
;
;;; Test the guesses
;guess-1
;(good-enough? a guess-1)
;guess-2
;(good-enough? a guess-2)
;guess-3
;(good-enough? a guess-3)
;guess-4
;(good-enough? a guess-4)
;guess-5
;(good-enough? a guess-5)
;guess-6
;(good-enough? a guess-6)
;guess-7
;(good-enough? a guess-7)
;guess-8
;(good-enough? a guess-8)
;guess-9
;(good-enough? a guess-9)
;guess-10
;(good-enough? a guess-10)
;