rorymckinley (1) [Avatar] Offline
#1
Can Referential Transparency be bastardised as "for a given set of inputs the function always returns the same output" ? If so, I am confused by why the code on pg 38 means that we have to have immutable data. In the example given, the variable list initially has a value of [2,5,7,2] and total([2,5,7,2]) = 16.

Then we add 5 to the list - and its total is now 21. In both cases, we happen to have a list called `list` - but as far as total() is concerned - every time you call it with [2,5,7,2] it returns 16 and every time you call it with [2,5,7,2,5] it returns 21.

I understand why immutable data is an important thing - but I do not understand why immutable data is a prerequisite to make the total function referentially transparent.
aslam.khan (50) [Avatar] Offline
#2
Re: Referential Transparency - I'm confused
No, referential transparency should not be bastardised as you suggest. It is the single characteristic that makes functions, well ... functions. In the case of the list, it is not that we have a list that is important but rather the values in the list. With referential transparency we really don't care about variables and references but about their values. So two lists are considered different because their values are different, not their references. In the example, then, the lists are different hence different outputs.

Lastly, immutable data is not pre-requisite for referential transparency but a desired convenience. If it was convenient to pass all inputs by value, not reference, then we can a relax a little bit on immutability. Inside the function, we still must take care not to mutate values too (same reason). So, in the end ... either we take care not to mutate or have the language help us out.
jfs.world (109) [Avatar] Offline
#3
Re: Referential Transparency - I'm confused
Hang on. I'm a bit confused. Are you agreeing? or disagreeing?

In chapter 2, "PAUSE: Where are we?", I see
=======
3 A function is referentially transparent
A function outputs the same value for the same inputs
======