Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the function println not pure? (Clojure)

It always returns nil.

Doesn't that mean it's pure?

Does println cause any side effects??

like image 752
toreohm Avatar asked Nov 29 '17 21:11

toreohm


2 Answers

Side effects can be roughly defined as "modifying any state not contained within the function itself". This includes writing to disk or making some sort of external API call, etc. Since println writes to STDOUT, it is altering the state of the stdout. Therefore it has a side effect.

like image 70
cwallenpoole Avatar answered Sep 23 '22 11:09

cwallenpoole


It always returns nil.

Doesn't that mean it's pure?

It only means it's pure if it doesn't have side-effects.

Does println cause any side effects??

Yes, it prints.

If it didn't have a side-effect, it would be pretty boring since it also doesn't return anything sensible.

I prefer the term referential transparency to purity. An expression or a function is referentially transparent if you can replace it with its value (or vice versa) without changing the meaning of the program. This means that println is referantially transparent IFF I can replace any occurrence of println with nil or any occurrence of nil with println without changing the meaning of the program. Obviously, you can't do that, ergo, println is not referentially transparent / pure.

like image 31
Jörg W Mittag Avatar answered Sep 23 '22 11:09

Jörg W Mittag