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]

Paul Moore <p.f.moore at gmail.com>: > On Fri, 7 Sep 2018 at 15:10, Paul Moore <p.f.moore at gmail.com> wrote: >> >> On Fri, 7 Sep 2018 at 14:06, Steven D'Aprano >> <steve+comp.lang.python at pearwood.info> wrote: >> > However I have a follow up question. Why the "let" construct in the >> > first place? Is this just a matter of principle, "put everything in >> > its own scope as a matter of precautionary code hygiene"? Because I >> > can't see any advantage to the inner function: >> >> My impression is that this is just functional programming "good >> style". [...] > > It's also worth noting that functional languages don't typically have > variables or assignments (more accurately, such things aren't > fundamental to the programming model the way they are in imperative > languages). So although technically let introduces a new scope, in > practical terms it's basically just "how functional programmers do > assignments". To put it succinctly, SML does it because there's no other way to introduce local variables. IOW, in many functional programming languages, the only local variables are function arguments. And if you want to go really functional, you can't even alter bindings. So to implement a for loop in Python under these constraints, you would implement: def f(n): sum = 0 m = 1 for i in range(n): sum += m * i if sum % m == 0: m += 1 return sum like this: def f(n): def auxf1(sum, m, i): if i == n: return sum else: def auxf2(sum, m, i): if sum % m == 0: return auxf1(sum, m + 1, i) else: return auxf1(sum, m, i) return auxf2(sum + m * i, m, i + 1) return auxf1(0, 1, 0) cheating slightly with locally named functions. If cheating is not allowed, you will need a Y combinator construct... Marko

- Prev by Date:
**Any SML coders able to translate this to Python?** - Next by Date:
**I need help to put the output from terminal in a csv 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):