Hacker News new | ask | show | jobs
by nopuremore 4082 days ago
The purpose of defining a function as pure is to allow the computer to make transformations. If you define println as a pure function and use pure to debug or trace your program you could discover that the compiler has eliminated some dead code and that some code has been executed out of order, so your debugging doesn't has any useful meaning. That's why is better to tell you that print isn't pure, the compiler could eliminate it if you define it as pure.

Disclaimer: I only has read the first comments.

1 comments

In Haskell this goes even deeper because the lazy evaluation strategy allows the compiler to optimize the order of evaluation of the expressions within a function and create thunks without you explicitly telling it to. Haskell needs to be pure in order to be lazy, because if it were impure it would be very difficult to reason about if, when, and in what order, the side effecting operations will take place. So like you said, if a side-effectful expression evaluates to nothing, its return value is not "needed" in a further computation, therefore it may not be evaluated at all.

This doesn't really apply to Clojure because Clojure is strictly evaluated--except when you're explicitly working with a lazy stream data structure. If you were to put side-effectful expressions inside of a lazy stream, you would have a hard time controlling when they are evaluated, especially since Clojure "chunks" lazy streams by default in groups of 32 as an optimization.

Here's a SO question demonstrating what happens when you mix laziness and side-effects and don't understand the implications--you find yourself trying to restrict the optimizations you allow the compiler to do (which will hurt performance): http://stackoverflow.com/questions/3407876/how-do-i-avoid-cl...