Comment by elbear
17 days ago
Ok, you're probably proving the point that purity also requires immutability. I'm not sure, as I haven't considered all the implications of Haskell's design.
My two rules about inputs and outputs are more like heuristics. They can improve code organisation and probably also decrease the likelihood of some errors, but they don't guarantee correctness, as you're pointing out. They're shortcuts, so they're not perfect.
Edit: If I remember right, it's laziness that requires immutability. I think I read something about this in the Haskell subreddit as an explanation for Haskell's design.
Even without laziness, you can get similar problems if f creates a closure or returns something that includes the parameter object.
Ok, the Wikipedia definition of pure function is more strict and than what I was saying and I think it covers the issues you mentioned:
https://en.wikipedia.org/wiki/Pure_function
Yeah, this is a common source of confusion with closures in Python. Example on Stack Overflow:
https://stackoverflow.com/questions/233673/how-do-lexical-cl...
Doesn't that example also show a kind of laziness?
I say this because the second solution to that question offers the solution of using `i` as a default argument when defining the function. That forces its evaluation and fixes the problem.
2 replies →