Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of scope does Haskell use?

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.

like image 931
Sev Avatar asked Dec 02 '08 06:12

Sev


2 Answers

Haskell use (broadly speaking) exactly the same lexical scoping as most other languages.

eg.

x = 10

Results in a value referenced through x in the global scope, whereas

square x = x * x

will 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:

square = \ x -> x * x

As to your other question i'm not sure what you mean by aliasing

like image 119
olliej Avatar answered Sep 21 '22 05:09

olliej


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:

foo x y = x * y
bar z = foo z z

When within foo called from bar, both x and y are clearly the same value. But since you cannot modify either x or y, you will not even notice.

like image 32
CesarB Avatar answered Sep 22 '22 05:09

CesarB