From 95018134a8a430d4c43b4b1f5bbd552e64853157 Mon Sep 17 00:00:00 2001 From: Yehonathan Sharvit Date: Fri, 25 Mar 2016 08:30:23 +0200 Subject: [PATCH 1/3] introduce KLIPSE in section 1.1.7 --- source/section/1.1.7.html.md | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/source/section/1.1.7.html.md b/source/section/1.1.7.html.md index 26a4409..9392231 100644 --- a/source/section/1.1.7.html.md +++ b/source/section/1.1.7.html.md @@ -103,22 +103,11 @@ always guess that the square root of any number is 1 (sqrt-iter 1.0 x)) ``` -If we type these definitions to the interpreter, we can use -sqrt just as we can use any function: - -``` -> (sqrt 9) -3.00009155413138 - -> (sqrt (+ 100 37)) -11.704699917758145 - -> (sqrt (+ (sqrt 2) (sqrt 3))) -1.7739279023207892 - -> (square (sqrt 1000)) -1000.000369924366 -``` +Now, let's play with it: + The sqrt `program` also illustrates that the simple procedural language we have introduced so far is sufficient for writing any From 65c5f21e3020855f733ddf951665616b53633b9e Mon Sep 17 00:00:00 2001 From: Yehonathan Sharvit Date: Tue, 17 May 2016 07:58:26 +0300 Subject: [PATCH 2/3] Revert "introduce KLIPSE in section 1.1.7" This reverts commit 95018134a8a430d4c43b4b1f5bbd552e64853157. --- source/section/1.1.7.html.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/source/section/1.1.7.html.md b/source/section/1.1.7.html.md index 9392231..26a4409 100644 --- a/source/section/1.1.7.html.md +++ b/source/section/1.1.7.html.md @@ -103,11 +103,22 @@ always guess that the square root of any number is 1 (sqrt-iter 1.0 x)) ``` -Now, let's play with it: - +If we type these definitions to the interpreter, we can use +sqrt just as we can use any function: + +``` +> (sqrt 9) +3.00009155413138 + +> (sqrt (+ 100 37)) +11.704699917758145 + +> (sqrt (+ (sqrt 2) (sqrt 3))) +1.7739279023207892 + +> (square (sqrt 1000)) +1000.000369924366 +``` The sqrt `program` also illustrates that the simple procedural language we have introduced so far is sufficient for writing any From 9f5210aae9cc645d1e3aa7c9f1e4402fd0e44c8e Mon Sep 17 00:00:00 2001 From: Yehonathan Sharvit Date: Tue, 17 May 2016 09:13:44 +0300 Subject: [PATCH 3/3] klipse plugin integrated on section 1.1.7 --- source/section/1.1.7.html.md | 54 +++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/source/section/1.1.7.html.md b/source/section/1.1.7.html.md index 26a4409..17e0a2b 100644 --- a/source/section/1.1.7.html.md +++ b/source/section/1.1.7.html.md @@ -61,7 +61,7 @@ compute) and a value for the guess. If the guess is good enough for our purposes, we are done; if not, we must repeat the process with an improved guess. We write this basic strategy as a function: -``` +```clojure (defn sqrt-iter [guess x] (if (good-enough? guess x) guess @@ -71,7 +71,7 @@ improved guess. We write this basic strategy as a function: A guess is improved by averaging it with the quotient of the radicand and the old guess: -``` +```clojure (defn improve [guess x] (average guess (/ x guess))) @@ -86,11 +86,12 @@ differs from the radicand by less than a predetermined tolerance (here 0.001) ```clojure +(defn square [x] +(* x x)) + (defn good-enough? [guess x] (< (abs (- (square guess) x)) 0.001)) -``` -```clojure (defn abs [n] (max n (- n))) ``` @@ -98,7 +99,7 @@ differs from the radicand by less than a predetermined tolerance (here Finally, we need a way to get started. For instance, we can always guess that the square root of any number is 1 -``` +```clojure (defn sqrt [x] (sqrt-iter 1.0 x)) ``` @@ -106,20 +107,23 @@ always guess that the square root of any number is 1 If we type these definitions to the interpreter, we can use sqrt just as we can use any function: +```clojure +(sqrt 9) ``` -> (sqrt 9) -3.00009155413138 -> (sqrt (+ 100 37)) -11.704699917758145 +```clojure +(sqrt (+ 100 37)) +``` -> (sqrt (+ (sqrt 2) (sqrt 3))) -1.7739279023207892 +```clojure +(sqrt (+ (sqrt 2) (sqrt 3))) +``` -> (square (sqrt 1000)) -1000.000369924366 +```clojure +(square (sqrt 1000)) ``` + The sqrt `program` also illustrates that the simple procedural language we have introduced so far is sufficient for writing any purely numerical program that one could write in, say, C or @@ -150,18 +154,18 @@ be done, and she defines a new version of if : Eva demonstrates the program for Alyssa: +```clojure +(new-if (= 2 3) 0 5) ``` -> (new-if (= 2 3) 0 5) -5 -> (new-if (= 1 1) 0 5) -0 +```clojure +(new-if (= 1 1) 0 5) ``` Delighted, Alyssa uses new-if to rewrite the squareroot program: -``` -(defn sqrt-iter [guess x] +```clojure +(defn sqrt-iter-with-new-if [guess x] (new-if (good-enough? guess x) guess (recur (improve guess x) x))) @@ -185,3 +189,15 @@ change is a very small fraction of the guess. Design a square-root function that uses this kind of end test. Does this work better for small and large numbers? + + + + + + + +