Clojure Functions of this article

(defn square [x] (* x x))

(defn abs [x] (if (< x 0) (- x) x ))

(defn average [x y] (/ (+ x y) 2))

(defn improve [guess x] (average guess (/ x guess)))

(defn good-enough? [guess x] (< (abs (- (square guess) x)) 0.001))

(defn sqrt-iter [guess x] (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x) ))

(defn sqrt [x] (sqrt-iter 1.0 x))

Now you can run the examples.

- bmentges (Bruno Carvalho)

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <hr> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options