[Haskell-beginners] Concatenating lists
Thomas Davie
tom.davie at gmail.com
Thu Mar 22 17:11:43 CET 2012
The logical issue you're hitting is mostly because the REPL (ghci most likely) is simulating something that you should ignore for the moment (the IO monad).
In Haskell, typically, you don't define one thing, then define another thing "later". A definition in a Haskell program doesn't assign a value, it binds a value. While assignment says "a now has the value 5 + 3", binding says "a always has, now has, and always will have the value 5+3".
A Haskell program like this that binds the same value twice is simply incorrect:
a = 5+3
a = 5+4
There are two competing facts here.
When typing into the REPL, you can rebind values, because each subsequent line appears (at least in the logical model) in an inner definition to the past. This is how scoping comes into it – the later (and hence inner) let takes precedence because it is in a closer scope.
My advice to you would be to stop thinking of trying to re-assign values right now, it's not the correct way to think about a Haskell program at all.
Bob
if (*ra4 != 0xffc78948) { return false; }
On 22 Mar 2012, at 15:54, Jan Erik Moström wrote:
> On 2012-03-22 at 16:12 , Felipe Almeida Lessa wrote:
>> Your conclusion is correct, but your reasoning is not. It's not due
>> to lazyness that x is recursively defined, it's because of the scope.
>> When you type your second x on the second line, that x will refer to
>> the closest (in scope) definition of something called x -- which is
>> the the definition of x on the second line itself.
>
> Aha, thanks for explaining. I didn't consider scope - I did some searching of scoping and need to think about that for a while, a bit different than 'normal'. However, I would like to ask one more thing:
>
> Assume
>
> let a = 5 + 3
>
> Would 5+3 be calculated directly or would it be deferred to later?
> Assume it's calculated and the value 8 is given the name 'a'. Then
>
> let a = a + 4
>
> What happens is that Haskell tries to evaluate the second 'a' on that line and then finds the first 'a' and enters into infinite recursion.
>
> And this is what confuses me a little bit. I can type in 'let a = a + 4' and Haskell accepts it, the infinite recursion does not happen until I type 'a' to show the value, this makes me think that the evaluation is deferred to later (and also why I asked about 5+3). Is this correct?
>
> - jem
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120322/f523ae24/attachment-0001.htm>
More information about the Beginners
mailing list