immutability - what's "immutable variable" means in functional programming -


i'm new in functional programming, , can not understand concept of "immutable".
example, in sml:

val = 3 val = a+1 

according principle of sml, last line not "change" value of variable a, equals 4 now, can please explain me ? , what's benefit of "no mutation"?

when variable immutable, mean own value cannot changed. you're showing there

val = 3 val = a+1 

is: new value of a "shadowing" old value of a. a name bound 3, , in second line, it's bound 4. old value of a still exists, it's inaccessible.

this can seen more apparently if you're using sort of data structures. there's no mutator methods see in many other languages. example, if have list val l = [1,2,3], there's no way change first value in l. have shadow l altogether, , create new list shadow old one.

so, every time bind new value declaration creates new environment current name/value bindings. none of these bindings can changed, they're shadowed.


Comments