1.

What do you mean by referential transparency in functional programming?

Answer»

In functional programming, referential transparency is the KEY differentiating factor.  An expression is considered referential TRANSPARENT if it can be replaced or SUBSTITUTED with the corresponding value it computes or vice-versa without affecting the PROGRAM’s result.

Example: Imagine that we have an expression called four: val four= add(1,3)

If four is used anywhere in our code, it can safely be replaced with add(1,3), 1 + 3 or 4 wherever it appears. Thus, all the EXPRESSIONS below are equivalent in meaning and output:

val eight = four + fourval eight_v2 = add(1,3) + add(1,3)val eight_v3 = 4 + add(1,3)val eight_v4 = 8

If we can swap back-and-forth between these expressions anytime, anywhere, without altering their meaning or output, then an expression is referentially transparent.



Discussion

No Comment Found