In slide 7 in Haskell 101, it uses the following example to show immutability:
I think we need a better example than this one. Because of the following reasons:
(1) This is syntactically wrong. And the syntactic failure has nothing to do with immutability. We can, for example, rewrite it to
and it just fails.
(2) We can, actually, slightly revise the example to:
let a = 3
in let a = 4
in a + 1
This works. But this is, of course, related to shadowing rather than immutability. Then this example is quite confusing.
(3) We can, actually, further revise the example to:
let a = 3
in let a = a + 1
in a + 1
This works. But it just loops forever, because in the definition a = a + 1, the a being defined is brought into the scope of a + 1. So it just recurs forever. Again, this example is being confusing.
In slide 7 in Haskell 101, it uses the following example to show immutability:
I think we need a better example than this one. Because of the following reasons:
(1) This is syntactically wrong. And the syntactic failure has nothing to do with immutability. We can, for example, rewrite it to
and it just fails.
(2) We can, actually, slightly revise the example to:
This works. But this is, of course, related to shadowing rather than immutability. Then this example is quite confusing.
(3) We can, actually, further revise the example to:
This works. But it just loops forever, because in the definition
a = a + 1, theabeing defined is brought into the scope ofa + 1. So it just recurs forever. Again, this example is being confusing.