Swapping the values of two variables, imperative solution: PROC swap(VAR x,y: int) VAR z: int BEGIN z := y; y := x; x := z END; with no extra memory: PROC swap(VAR x,y: int) {state} BEGIN {x=a,y=b} x := x+y; {x=a+b,y=b} y := x-y; {x=a+b,y=a} x := x-y {x=b,y=a} END; VAR z: int; BEGIN z:=3; {z=3} swap(z,z); {z=0} !!! ... END Moral: assignment (more in general, side-effects changing the state) plus aliasing is difficult. functional program: - fun swap(x,y) = (y,x); val swap = fn : 'a * 'b -> 'b * 'a - swap(2,true); val it = (true,2) : bool * int - swap(swap(1,2)); val it = (1,2) : int * int logic program: swap(pair(X,Y),pair(Y,X)). ?- swap(pair(2,true),Z). Z = pair(true,2) ?- swap(Z,pair(2,3)). Z = pair(3,2) ?- swap(pair(1,X),pair(2,Y)). X = 2 Y = 1 ?- swap(pair(1,X),pair(Y,Z)). X = Y Z = 1 ?- swap(pair(1,2),pair(Y,Y)). No