I'm trying to figure out if Haskell uses dynamic or static scoping. I realize that, for example, if you define:
let x = 10
then define the function
let square x = x*x
You have 2 different "x's", and does that mean it is dynamically scoped? If not, what scoping does it use, and why?
Also, can Haskell variables have aliases (a different name for the same memory location/value)?
Thanks.
Yes, Haskell has aliases. Try out this little program:
In your example, the global definition of x is shadowed by the local definition of x. In Haskell, a variable's scope is determined by a static reading of the source code - this is called lexical scope, but can get something similar to dynamic scoping with implicit parameters (but that can lead to some unexpected behavior (I've read; never tried 'em myself)).
Haskell use (broadly speaking) exactly the same lexical scoping as most other languages.
eg.
Results in a value referenced through
x
in the global scope, whereaswill result in
x
being lexically scoped to the function square. It may help if you think of the above form being a syntactic nicety for:As to your other question i'm not sure what you mean by aliasing