Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purely Functional Languages - Referential Transparency I am having problems unde

ID: 3566659 • Letter: P

Question

Purely Functional Languages - Referential Transparency

I am having problems understanding this statement:

"A purely functional programming language does not use variables or
assignment statements, thus freeing the programmer from concerns related to
the memory cells, or state, of the program. Without variables, iterative constructs
are not possible, for they are controlled by variables. Repetition must
be specified with recursion rather than with iteration. Programs are function
definitions and function application specifications, and executions consist of
evaluating function applications. Without variables, the execution of a purely
functional program has no state in the sense of operational and denotational
semantics. The execution of a function always produces the same result when
given the same parameters. This feature is called referential transparency. It
makes the semantics of purely functional languages far simpler than the semantics
of the imperative languages"

Does a purely functional languages enjoy/does not enjoy the property of referential transparency?

Explanation / Answer

Purely functional functions (or expressions) have no side effects (memory or I/O). This means that pure functions have several useful properties, many of which can be used to optimize the code:

While most compilers for imperative programming languages detect pure functions and perform common-subexpression elimination for pure function calls, they cannot always do this for pre-compiled libraries, which generally do not expose this information, thus preventing optimizations that involve those external functions. Some compilers, such as gcc, add extra keywords for a programmer to explicitly mark external functions as pure, to enable such optimizations. Fortran 95 also allows functions to be designated "pure".

Pure computations yield the same value each time they are invoked. This property is called referential transparency and makes possible to conduct equational reasoning on the code. For instance if

y = f x

and

g= h y y

then we should be able to replace the definition of

g

with

g = h (f x) (f x)

and get the same result; only the efficiency might change.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote