0
$\begingroup$

I have a rather foolish question on an example explaining the idea behind Referential transparency

Here is given an example i not understand:

Consider a function that returns the input from some source. In pseudocode, a call to this function might be GetInput(Source) where Source might identify a particular disk file, the keyboard, etc. Even with identical values of Source, the successive return values will be different. Therefore, function GetInput() is neither deterministic nor referentially transparent.

I'm confused about the claim that even with identical values of (variable) Source, the successive return values will be different.

Eg if I tap an 'a' on my keyboard and apply GetInput(Source), it gives my 'a' as output. Then I tap again an 'a', which of course is identical to my previuos input. Next application of GetInput(Source) gives me again 'a'.

Therefore I not understand how is it possible that GetInput(Source1) and GetInput(Source2) might give different values if the two imputs Source1 and Source2 are identical. Sorry, if it's too dumb, but I not understand this quoted example.

$\endgroup$

1 Answer 1

1
$\begingroup$

If Source refers to the keyboard, and the user types a the first time but b the second time, then GetInput(Source) will not return the same value both times.

It's impossible to replace GetInput(Source) by its value without changing the meaning of the program, because there are many possible values, depending on the actions of the user. To replace GetInput(Source) with a would be wrong if the user decides to press b.

An example of a consequence of the fact that GetInput(Source) is not referentially transparent is that if the program calls this twice, which call returns a and which calls return b depends on the order in which the calls are evaluated (or more precisely, on the order in which the calls get their input from the keyboard: you have to get down to this level of detail to be sure).

$\endgroup$
1
  • $\begingroup$ oh yes, you are right, I confused that Source is the variable of the input devise, not the input itself, that's just the point, right? $\endgroup$
    – user267839
    Apr 27, 2021 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.