|20-CS-4003-001||Organization of Programming Languages||Fall 2017|
(let ((a (+ 1 2 3 4))) (set! a 1) a)
a = [1,2,3,4]
a = new Vector
Scheme is a functional language that has some extensions which allow
assignment. Assignments are convenient but can have serious consequences
pertaining to code correctness: it is harder to write correct code if
variable values or the contents of data structures can change at any time.
The let in the code at the right is considered safe because it
associates a value with a variable for the duration of execution of the
let block. The set! is a destructive change in the
value of the variable a. The value of the let expression
is 1, not 15.
In Haskell there is no assignment. The statement to the left has the same function as the let in Scheme. It is said to be a declaration in Haskell. In Haskell, changes in state are handled by producing a new one from the old one and using the new in place of the old one. Examples will follow. In a functional language the effect of any expression on the overall computation of a program is limited to the value that it returns.
In Java, an imperative language, assignment is routine. It is difficult to imagine how to solve problems without assignment. In an imperative language expressions are typically designed to make changes to the state of memory.