Iterate help in Clojure - clojure

I'm new to Clojure and am trying to write a Newton's method function using the built in iterate. I have tried several things that resulted in the following code. Any ideas as to why this is only returning empty parens? I'm open to new ideas as well.
(defn within-tol?
"returns true if the guess is within the given tolerance"
[guess x tolerance]
(< (abs (- (square guess) x)) tolerance))
(defn next-guess
[guess x]
(average guess (/ x guess)))
(defn average
[x y]
(float (/ (+ x y) 2)))
(defn abs
[x]
(cond
(< x 0) (- x)
:else x))
(defn square
[x]
(* x x))
(defn sqrt-iter
[guess x tolerance]
(if (within-tol? guess x tolerance) guess
(sqrt-iter (next-guess guess x) x tolerance)))
(defn sqrt
[guess x tolerance]
(take-while #(within-tol? % x tolerance) (iterate #((sqrt % x tolerance)) guess)))

Seems your sqrt is wrong as it does not use next-guess
Try that
(defn sqrt
[guess x tolerance]
(first (drop-while #(not (within-tol? % x tolerance))
(iterate #(next-guess % x) guess))))
Example:
(sqrt 1 169 0.01) => 13.0

Related

Why is this function not evaluating

I have been stuck on this one for a while and cant figure it out. Why is the function not evaluating for values greater or equal than 2? It works fine for values under it but anything above and it just prints the output seen in the pic
((fn fib
([x] (cond
(zero? x) []
(= x 1) [1]
(>= x 2) fib (- x 2) [1 1]))
([x seq] (if (zero? x)
seq
(recur (dec x) (conj seq (+ (last seq) (nth seq (- (count seq) 2)))))))) 3)
enter image description here
You forgot one parenthesis on this line, before fib:
(>= x 2) fib (- x 2) [1 1]))
After you will add it, this code will work. Just note that your variables shouldn't have the same name as already existing functions- you were shadowing core function seq.
((fn fib
([x] (cond
(zero? x) []
(= x 1) [1]
(>= x 2) (fib (- x 2) [1 1])))
([x values]
(if (zero? x)
values
(recur (dec x)
(conj values (+ (last values)
(nth values (- (count values) 2)))))))) 3)

Get lost in parentheses (Clojure)

Please comment how to restructure the following function more readable and less parentheses.
(defn forwardDifference2nd [fn x h]
(->
(/ (+ (-
(fn (+ (+ x h) h))
(* (fn (+ x h)) 2.0))
(fn x))
(Math/pow h 2))))
Source Code
(def x 1.0)
(def h 0.1)
(defn forwardDifference2nd [fn x h]
(->
(/ (+ (-
(fn (+ (+ x h) h))
(* (fn (+ x h)) 2.0))
(fn x))
(Math/pow h 2))))
(defn myPolynomial [x]
(->
(+ (* 2
(Math/pow x 3))
(* 4
(Math/pow x 2)))
(- (* 5 x))))
(forwardDifference2nd myPolynomial x h)
Here is one example:
(defn forwardDifference2nd [f x h]
(let [fh #(f (+ x (* h %)))]
(/ (+ (fh 2) (* -2 (fh 1)) (f x))
h h)))
Simplifications:
Remove (-> ...)
Factor out similar looking expressions into local function
Flatten nested sums and differences
Replace (/ ... (Math/pow h 2)) by (/ ... h h)
Update:
Your computation could more generally be expressed as
(defn difference-at-depth [f x h depth]
(if (zero? depth)
(f x)
(let [depth (dec depth)]
(/ (- (difference-at-depth f (+ x h) h depth)
(difference-at-depth f x h depth))
h))))
(defn forwardDifference2nd [f x h]
(difference-at-depth f x h 2))
but this code is obviously longer. On the other hand, it better shows what is going on mathematically and is in that sense arguably more readable.
Locally naming with let and then using named parts for calculation not only enhances readability of formulas but also is computationally more efficient.
For re-occuring patterns like a polynom in polynomial, define a function with a name.
(defn forward-difference-2nd [f x h]
(let [f_xhh (f (+ x h h))
f_xh (f (+ x h))
f_x (f x)
h**2 (* h h)]
(/ (- f_xhh f_xh f_xh f_x) h**2)))
(defn polynom [coeff x power] (* coeff (Math/pow x power)))
(defn my-polynomial [x]
(+ (polynom 2 x 4) (polynom 4 x 2) (polynom -5 x 1)))
(def x 1.0)
(def h 0.1)
(forward-difference-2nd my-polynomial x h)

Loop destructuring in Clojure

Is there a way for destructuring the recur bindings in a loop?
So for example if i want to let y stay unchanged in
(loop [x 1 y 1] (if (< x 5) (recur (inc x) _ ) (println "result:" x y))
I know this is not possible, but how could i get a similar thing like _ in sequential destructuring for recur?
Assuming y never changes inside the loop, you can just wrap everything in a let that binds a value to y, or even place your loop in a function that receives y as an argument.
let
(let [y 1]
(loop [x 1]
(if (< x 5)
(recur (inc x))
(println "result:" x y)))
defn
(defn loop-with [y]
(loop [x 1]
(if (< x 5)
(recur (inc x))
(println "result:" x y)))

Returning a value from a loop

Is there anyway i can return a value from a loop since the recursion has to be at the tail
(ns for)
(defn abc [y]
(loop [x 10]
(when (> x 2)
(if (= 2 3) (do (println "test") (recur (- x 2)))
(do (let [x (+ 1 x)
y 2] (println y) (recur (- x 2))))))))
(abc 1)
is there anyway i can return a value for the function by taking y as a parameter and updating a new value of y. However, the recur part has to be at the last line of the code hence i am unable to put y as the last line of the code.
Example
(ns for)
(defn abc [y]
(loop [x 10]
(when (> x 2)
(if (= 2 3) (do (println "test") (recur (- x 2)))
(do (let [x (+ 1 x)
y 2] (println y) (recur (- x 2)))))))
y)
(abc 1)
This would give me an error since recur has to be the last line of code. I have looked at similar questions and it says to put the return value at the end of the if loop which i tried but failed which gives me an exception thatthe recursion can only happen at the tail
I guess you meant this:
(defn abc [y]
(loop [x 10
y nil]
(if (> x 2)
(if (= 2 3)
(do (println "test")
(recur (- x 2) nil))
(do (let [x (+ 1 x)
y 2]
(println y)
(recur (- x 2) y))))
y)))
Update. Without unnecessary parts it would be
(defn abc [y]
(loop [x 10]
(if (> x 2)
(do (println 2)
(recur (- x 1)))
2)))
which is the same as
(defn abc [_]
(dotimes [_ 8] (println 2))
2)

putting clojure code in a loop

(def throws 10)
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(loop [hits 0]
(let [x (rand)
y (rand)]
; still inside the let
(if (< (r-squared x y) 0.25) ;is it a hit or not? if not, try again
(recur (inc hits))
(* 4 (/ hits throws)))))
I got that code working and running until the if condition is true. How can I rewrite it so it takes X as parameter and runs X times?
I basically want to call (r-squared 100) and get how many hits I got as return value.
I think this is what you want, if understend question correctly.
(defn test [n]
(loop [hits 0 n n]
(let [x (rand)
y (rand)]
(if (< n 0)
hits ;// you can put (* 4 (/ hits throws)) here
(if (< (r-squared x y) 0.25)
(recur (inc hits) (dec n))
(recur hits (dec n)))))))
Didn't eval it, so parn might be slightly wrong.
(def throws 10)
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(defn test-r-squared [n]
(loop [hits (int 0) n (int n)]
(let [x (rand)
y (rand)]
; still inside the let
(if (< n 0)
(* 4 (/ hits throws))
(if (< (r-squared x y) 0.25) ;is it a hit or not? if not, try again
(recur (inc hits) (dec n))
(recur hits (dec n)))))))
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(defn hit[]
(let [x (rand) y (rand)]
(< (r-squared x y) 0.25)))
(frequencies (for [i (range 1000)] (hit))) ; {true 787, false 213}