[Haskell-beginners] haskell variables - or not

Thomas Davie tom.davie at gmail.com
Sat Jul 25 03:56:04 EDT 2009


On 25 Jul 2009, at 06:25, Duke Normandin wrote:

> Hello....
>
> I'm 2 days into Haskell. NOT new to Perl, PHP, M(umps), TCL,  
> newLISP. I'm
> using "YAHT.pdf" as an intro to Haskell. Here's a quote:
>
> [quote]
> ... if you have a value x, you must not think of x as a register, a
> memory location or anything else of that nature. x is simply a name,  
> just as
> Hal  is my name. You cannot arbitrarily decide to store a different  
> person in
> my name any more than you can arbitrarily decide to store a  
> different value in
> x. This means that code that might look like the following C code is  
> invalid
> (and has no counterpart) in Haskell:
>
> int x = 5;
> [/quote]
>
> So is fair to say that an expression in Haskell like:
>
> x = 5
>
> is more like a constant in imperative languages - say Perl? E.g.
>
> use constant SECONDS_PER_DAY => 86400;

x is indeed a constant in this case.  The = sign in Haskell is used  
not for "assignment" as in most imperative programming languages, but  
instead for "binding" as in most mathematics.  It says "x is equal to  
5", "x always has been equal to 5", "x will always be equal to 5".   
The reason why these are still called variables is from when they are  
used as arguments to functions:

f y = y + 2

Here, we are "binding" f.  f always was, will be and is the function  
which adds two to its argument, but it's argument varies depending on  
the call, and thus is a variable.

Hope that helps.

Bob


More information about the Beginners mailing list