Re:An Experiment in Purely Functional IO for Clojure
by sgk284 on Sunday April 19, 2015 @03:43AM
(#9402358)
> It always returns nil (for inputs that it doesn't fail on).
> Doesn't that mean it's pure?
Unfortunately not because it also modifies global state (the state of the console). A pure function implies that it is referentially transparent, meaning that:
a = foo()
b = a + a
Is the exact same as:
b = foo() + foo()
But if foo() modifies global state, then this statement is not true. In this case the difference is printing something twice vs. once. If a function is pure, a compiler can optimize the second example into the first example.
For a function to be pure, it must always return the same value given the same input and not modify any state that is observable outside of its definition.
> Doesn't that mean it's pure?
Unfortunately not because it also modifies global state (the state of the console). A pure function implies that it is referentially transparent, meaning that:
Is the exact same as: But if foo() modifies global state, then this statement is not true. In this case the difference is printing something twice vs. once. If a function is pure, a compiler can optimize the second example into the first example.For a function to be pure, it must always return the same value given the same input and not modify any state that is observable outside of its definition.