4 ms·
> 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 stat
by sgk284 12y ago
> 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.