added assignment 7 ex 4

This commit is contained in:
Louis Heredero 2025-04-16 07:53:28 +02:00
parent 17c1e4170d
commit 95fdbf7abf
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

19
src/Assignment7/Ex4.sc Normal file
View File

@ -0,0 +1,19 @@
def THRESHOLD: Double = 0.0001
def sqr(x: Double): Double = x * x
def sqrt_stream(value: Double): LazyList[Double] = {
def helper(target: Double, approx: Double): LazyList[Double] = {
approx #:: helper(target, approx - (sqr(approx) - target) / (2 * approx))
}
helper(value, value)
}
sqrt_stream(2).take(10).toList
def threshold(list: LazyList[Double], thresh: Double) = {
list.zip(list.drop(1)).filter(p => math.abs(p._2 - p._1) < thresh).head._2
}
threshold(sqrt_stream(2), 1e-15)