Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce KLIPSE (live coding) in section 1.1.7 #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions source/section/1.1.7.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))

Expand All @@ -86,40 +86,44 @@ 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)))
```

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))
```

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
Expand Down Expand Up @@ -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)))
Expand All @@ -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?

<link rel="stylesheet" type="text/css" href="http://app.klipse.tech/css/codemirror.css">

<script>
window.klipse_settings = {
selector: 'code.clojure'
};
</script>

<script src="http://app.klipse.tech/dev/js/klipse_plugin.js"></script>