Skip to content

Comment on An Experiment in Purely Functional IO for Clojureparent

Comments

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.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.