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.
There are some things wrong in your statements...
In your example, x is not 10 in the function is just a argument to square, that can take any value (you can specify the type later) in this case 10 but just in this case.
Here is an example of aliases provided by Curt Sampson:
As the first part of the question is already answered by others, here is the second part:
I assume by
aliasing
you meanone name for another
. As haskell is a functional language, and functions behave as normal identifiers in any case, you can do that like this:which would define an alias
y
for the functionx
. Note that everything is a function. Even if it looks like a "variable", it's just a nullary function taking no arguments. Aliases for types look like this:which would define an alias
Function
for the typeDouble -> Double
Haskell uses static nested scopes. What is a bit confusing compared with other languages that have static nested scopes is that the scope of a name is a block which includes tests preceding its definition. For example
here the name 'odds' is in scope in the definition of 'evens', despite the surprising fact that 'odds' has not yet been defined. (The example defines two infinite lists of even and odd numbers.)
A dead language with a similar scoping rule was Modula-3. But Haskell is a bit trickier in that you can attempt to 'redefine' a variable within the same scope but instead you just introduce another recursion equation. This is a pitfall for people who learned ML or Scheme first:
This is perfectly good ML or Scheme let*, but Haskel has scheme letrec semantics, without the restriction to lambda values. No wonder this is tricky stuff!
To sum up the other answers concisely:
x = 1; y = x
but doesn't usually matter because things are immutable.The
let
syntax you use in your example looks like it's at the interactiveghci>
prompt. Everything in interactive mode occurs within the IO monad so things may appear more mutable there than normal.Answering only the second part of the question:
You can have several aliases for the same "memory location", but since they are all immutable, it does not matter most of the time.
Dumb example:
When within
foo
called frombar
, bothx
andy
are clearly the same value. But since you cannot modify eitherx
ory
, you will not even notice.Well, as I think people have said already, Haskell doesn't have any variables as found in most other languages, it only has expressions. In your example
let x = 10
x is an expression that always evaluates to 10. You can't actually change the value of x later on, though you can use the scoping rules to hide it by defining x to be another expression.