haskell variables - or not

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; -- duke

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; -- duke
No. Think of x :: Int x = 5 more in terms of an expression rather than a variable. If you evaluate the expression x, say you would like to print its evaluation on the screen, then x is evaluated to the value 5. As the name of an expression needs to be unique in order to refer to it anywhere else in the program, you cannot have something like x :: Int x = 53 somewhere else in your code. Because then the expression x would be ambiguous. You will get the idea in a couple of days. I am sure. Best, Thomas
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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

Well, the short answer is sort of. The long answer is that you can't
really have variables declared outside of a function definition. When
you do something like:
x = 5
what you're really doing is declaring a new function x that takes no
arguments and returns the constant 5.
On Jul 25, 2009, at 12:25 AM, Duke Normandin
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; -- duke _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, 24 Jul 2009, 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; -- duke
Thanks to all the responders!! --Thomas F.
You will get the idea in a couple of days. I am sure.
Maybe next week! I'm going fly-fishing for trout on the weekend, as is my usual practice here in the Alberta (Canada) foothills ;) -- Kyle M. & Thomas D. What I gather from your replies is that the only "true" variables in Haskell are the args passed to a function. Is that correct? ...and further, that what sometimes appears to be a variable to an "unwashed" imperative programmer", is really a function with no arguments, returning a "constant" value. How did I do? -- duke

On Sat, Jul 25, 2009 at 8:32 AM,
On Fri, 24 Jul 2009, 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; -- duke
Thanks to all the responders!!
--Thomas F.
You will get the idea in a couple of days. I am sure.
Maybe next week! I'm going fly-fishing for trout on the weekend, as is my usual practice here in the Alberta (Canada) foothills ;)
-- Kyle M. & Thomas D.
What I gather from your replies is that the only "true" variables in Haskell are the args passed to a function. Is that correct?
...and further, that what sometimes appears to be a variable to an "unwashed" imperative programmer", is really a function with no arguments, returning a "constant" value. How did I do? -- duke
You got it. It's one of the hardest things to wrap your head around (well, that and Monads) but once you do the language starts to make a whole lot more sense. I can't claim I'm a master of it or anything as I'm pretty new to the language myself but it helps explain why what another commenter on this list tried to do didn't work. He was attempting to keep track of the number of calls to a function by doing: a = a + 1which once you realize a is a function and not a variable the stack overflow he was receiving makes sense as he created an infinitely recursive function (which incidentally isn't a problem until you attempt to evaluate it which he did on the very next line when he tried to print it). Haskell can support something like a variable outside of functions using these weird little monads like MVars and IORefs (among others), but those are manipulated as something like blackboxes using various functions to store and retrieve values out of them and to understand how they work you need to understand how Monads work (something I'm not entirely clear on myself). -Kyle Murphy
participants (5)
-
Duke Normandin
-
dukeofperl@ml1.net
-
Kyle Murphy
-
Thomas Davie
-
Thomas Friedrich