Jul-12-2019, 02:51 PM
I am attempting to create a sqrt function using mainly recursion with the use of a lambda.
I picked the code up from a book "Functional Python Programming 2ed".
The example is shown in the 1st chapter.
I picked the code up from a book "Functional Python Programming 2ed".
The example is shown in the 1st chapter.
#!/usr/bin/python3
def next_(n, x):
return (x+n/x)/2
n = 2 # converge value
f = lambda x: next_(n,x)
y=1.0 # seed value
def repeat(f, a):
yield a
for v in repeat(f, f(a)):
yield v
def within (tolerance, iterable):
def head_tail(tolerance, a, iterable):
b = next(iterable)
if abs(a-b) < tolerance: return b
return head_tail(tolerance , next(iterable), iterable)
def sqrt(y, tolerance, n):
return within(tolerance, repeat(f, y))
# show the square root of
print(sqrt(1.0, .0001, 3))the final print however shows up as None.
