-
Notifications
You must be signed in to change notification settings - Fork 126
Frequently Asked Questions
cedricss edited this page Aug 22, 2012
·
5 revisions
I have a record representing a user, for example:
baby = { name : "Alice", age : 1};
I tried this to change the age, but it doesn't work:
baby.age = 21
Here is the solution:
adult = { baby with age : 21 }
In an imperative language, where it would be valid to do this, it means the "baby" you defined at the beginning of your code can have its age changed elsewhere in the code: the object you would manipulate in the future has suddenly nothing to do with a baby and you have no clue it has changed. With a functional language you have the guarantee a value you defined can't be affected by other external function.
I have this initial users intmap:
users = IntMap.empty
My map is still empty after this:
IntMap.add(1, "Alice", users)
Solution:
users = IntMap.add(1, "Alice", users)
Understand why reading in the functional language section. Some related discussion about maps can be found here: