Marko Rauhamaa <[email protected]> (Marko Rauhamaa):
> Steven D'Aprano <[email protected]>:
>> I have this snippet of SML code which I'm trying to translate to Python:
>>
>> fun isqrt n = if n=0 then 0
>> else let val r = isqrt (n/4)
>> in
>> if n < (2*r+1)^2 then 2*r
>> else 2*r+1
>> end
> [...]
> You must make sure "r" doesn't leak outside its syntactic context so:
>
> def isqrt(n):
> if n == 0:
> return 0
> else:
> def f2398478957():
> r = isqrt(n//4)
> if n < (2*r+1)**2:
> return 2*r
> else:
> return 2*r+1
> return f2398478957()
Actually, this is a more direct translation:
def isqrt(n):
if n == 0:
return 0
else:
def f2398478957(r):
if n < (2*r+1)**2:
return 2*r
else:
return 2*r+1
return f2398478957(isqrt(n//4))
Marko
--
https://mail.python.org/mailman/listinfo/python-list