why is this legal

David Feuer David_Feuer@brown.edu
Sat, 2 Feb 2002 5:46:08 -0500


> From: Hal Daume III <hdaume@ISI.EDU>

> then, why are we allowed to rebind f in a let clause :)

1. There is no real reason not to allow this, so such a 
rule would just limit the flexibility of the language and 
annoy programmers.  It would also make it somewhat harder 
to write compilers, because they would have to check 
uselessly for violations of an extra rule.

2. It increases the ability to move code around without 
changing it.  For example, suppose you have

g = let f = 5 in ...
f x = ... g ....

Now let's say you realize you only ever use g in f.  So 
you have no need to clutter the top-level namespace with 
g.  So you can cut and paste to make

f x = ... (let f = 5 in ...) ...

There is another, possibly more significant reason, 
however:  sometimes it is very nice to be able to "shadow" 
an external variable (including perhaps the function being 
defined) in a function definition: it explicitly prevents 
you from using that variable by mistake.  This technique 
is often used to avoid bugs in loop code (though this is 
not specifically a reason to allow rebinding of the thing 
being defined...)

David Feuer

This message has been brought to you by the letter alpha and the number pi.