osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]# Any SML coders able to translate this to Python?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Steven D'Aprano <steve+comp.lang.python at pearwood.info>: > 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 > > > I've tried reading up on SML and can't make heads or tails of the > "let...in...end" construct. > > > The best I've come up with is this: > > def isqrt(n): > if n == 0: > return 0 > else: > r = isqrt(n/4) > if n < (2*r+1)**2: > return 2*r > else: > return 2*r+1 > > but I don't understand the let ... in part so I'm not sure if I'm doing > it right. 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() (Also use // instead of /: isqrt = integer square root.) Marko

- Prev by Date:
**Any SML coders able to translate this to Python?** - Next by Date:
**Verifying the integrity/lineage of a file** - Previous by thread:
**Any SML coders able to translate this to Python?** - Next by thread:
**Any SML coders able to translate this to Python?** - Index(es):