r/functionalprogramming • • Jan 24 '23

Question Example of a function that has referential transparency but is not pure?

I've read that Functions that have referential transparency can be replaced by their output. And also that Pure functions return the same result for the same input (which makes them referentially transparent) and don't have any effect on the rest of the program.

So what is an example of a function that has referential transparency but is not pure? Does a function which only depends on its inputs but modifies a global variable still have referential transparency? It wouldn't be pure from my understanding because it modifies other parts of the program.

19 Upvotes

25 comments sorted by

View all comments

Show parent comment

3

u/T_S_ Jan 24 '23

If you wish to redefine “pure” in this way, then you now have to distinguish between side effects that are safe and those that aren’t. That distinction will vary between applications and the subjective values of the users. Logs may be safe but “launch missiles” may not. Or even vice versa depending on who is using your code.