2 ms·
Consider the following program. var a = println("Hello ") a = println(" World") This should output "Hello \n World\n" However, assuming println
by gizmo686 12y ago
Consider the following program.
var a = println("Hello ")
a = println(" World")
This should output "Hello \n World\n"
However, assuming println is pure, we can optimize this to just
var a = println(" World")
Which produces a different output. We could also convert:
var a=println("Hello")
var b=println("Hello")
into
var a=println("Hello")
var b=a
- lgas 12y agoMy (quite possibly wrong) understanding is that purity has nothing to say about side effects. It's simply concerned with the inputs and outputs of functions, specifically whether the function will always return the same value given the same input. What zak_mc_kracken is saying is that under this definition println is obviously and trivially pure because it returns the same value for every input. You're correct that it won't have the proper side effects when optimized but under this definition that doesn't have anything to do with purity. Edit: If you want the "side effects" of the println statement to be considered part of the "output" then you want something like Haskell's semantics where the println statements are IO actions. Edit: zak_mc_kracken beat me to it.
- zak_mc_kracken 12y agoYou are correct under this particular interpretation of purity, there are just competing ways to assess said purity. Wikipedia only lists two (the one you just showed and the one I described) but is missing the third one (the inlining approach). In Haskell, all functions in the IO monad are by definition pure, and that includes all the println functions.