-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_1_06.clj
28 lines (22 loc) · 971 Bytes
/
ex_1_06.clj
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
(ns sicp.chapter-1.part-1.ex-1-06
(:require
[sicp.chapter-1.part-1.book-1-1 :as b11]))
; Exercise 1.6
; Alyssa P. Hacker doesn’t see why if needs to be provided as a special form.
; “Why can’t I just define it as an ordinary procedure in terms of cond?” she asks.
; Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:
(defn new-if
[predicate then-clause else-clause]
(cond predicate then-clause
:else else-clause))
; Eva demonstrates the program for Alyssa:
(new-if (= 2 3) 0 5) ; 5
(new-if (= 1 1) 0 5) ; 0
; Delighted, Alyssa uses new-if to rewrite the square-root program:
(defn sqrt-iter-new-if
[guess x]
(new-if (b11/good-enough? guess x)
guess
(b11/sqrt-iter (b11/improve guess x) x)))
; What happens when Alyssa attempts to use this to compute square roots? Explain.
; TODO Explain.