Question

In the context of Scala, briefly define what "referential transparency" means for expression evaluation and give...

In the context of Scala, briefly define what "referential transparency" means for expression evaluation and give an example to illustrate your answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Referential transparency:-

An expression is refrentially transparent if it can be replaced by its resulting value without changing the behaviour of the program regardless of where the expression is used in the program.

Refrentially transparent means the value of expression can depend only on the value of parts and not on any other facts about them.

For example it cannot depend on following

  • Whether some part of expression is already evaluted or not.
  • Whether two equal values are shared ( are pointers to the same location in memory) or not
  • Whether a data structure is cyclic (i.e its pointer create a cycle or not)

Example:-

For instance ,assume that a and b are immutable variables within same scope of application and within that scope they are used to form this expression

a+b

You can assign this expression to a third variable ,like this

val c=a+b

Now,throughout the given scope of your program,anywhere the expression a+b is used ,it can be replaced by c without affecting the result of the program.

today() is not transparent, as if you evaluate it and replace it by its value (say, "Jan 1, 2001"), you don't get the same result as you will if you run it tomorrow. This is because it depends on a state (the date).

Add a comment
Know the answer?
Add Answer to:
In the context of Scala, briefly define what "referential transparency" means for expression evaluation and give...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT