# Concatenating lists

7 messages
Open this post in threaded view
|

## Concatenating lists

 Hi, I've just started to learn Haskell and played around a bit and tried this 1: let x = [1,2,3] 2: let x = x ++ [4,5,6] 3: x The last line doesn't give a result. I assume that this is because 'x' is a name of a value, in the second line I redefine 'x' to a new value but Haskell doesn't evaluate the value until the last line but then 'x' becomes recursively defined in itself (the second x on line 2 is interpreted to refer to the value of the first x on line 2 and  not the value defined in line 1). This behavior is caused by the lazy evaluation in Haskell. Have I understood this correctly? - jem
Open this post in threaded view
|

## Concatenating lists

 On Thu, Mar 22, 2012 at 9:44 AM, Jan Erik Mostr?m wrote: > Hi, > > I've just started to learn Haskell and played around a bit and tried this > > 1: let x = [1,2,3] > 2: let x = x ++ [4,5,6] > 3: x > > The last line doesn't give a result. I assume that this is because 'x' is a name of a value, in the second line I redefine 'x' to a new value but Haskell doesn't evaluate the value until the last line but then 'x' becomes recursively defined in itself (the second x on line 2 is interpreted to refer to the value of the first x on line 2 and ?not the value defined in line 1). This behavior is caused by the lazy evaluation in Haskell. > > Have I understood this correctly? > Yeah, that sounds right. Antoine
Open this post in threaded view
|

## Concatenating lists

 In reply to this post by Jan Erik Moström On Thu, Mar 22, 2012 at 2:44 PM, Jan Erik Mostr?m wrote: > Hi, > > I've just started to learn Haskell and played around a bit and tried this > > 1: let x = [1,2,3] > 2: let x = x ++ [4,5,6] > 3: x > > The last line doesn't give a result. I assume that this is because 'x' is > a name of a value, in the second line I redefine 'x' to a new value but > Haskell doesn't evaluate the value until the last line but then 'x' becomes > recursively defined in itself (the second x on line 2 is interpreted to > refer to the value of the first x on line 2 and  not the value defined in > line 1). This behavior is caused by the lazy evaluation in Haskell. > > Have I understood this correctly? > Yes. > > - jem > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners> -------------- next part -------------- An HTML attachment was scrubbed... URL:
Open this post in threaded view
|

## Concatenating lists

 In reply to this post by Jan Erik Moström On Thu, Mar 22, 2012 at 11:44 AM, Jan Erik Mostr?m wrote: > Hi, > > I've just started to learn Haskell and played around a bit and tried this > > 1: let x = [1,2,3] > 2: let x = x ++ [4,5,6] > 3: x > > The last line doesn't give a result. I assume that this is because 'x' is a name of a value, in the second line I redefine 'x' to a new value but Haskell doesn't evaluate the value until the last line but then 'x' becomes recursively defined in itself (the second x on line 2 is interpreted to refer to the value of the first x on line 2 and ?not the value defined in line 1). This behavior is caused by the lazy evaluation in Haskell. > > Have I understood this correctly? 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. Cheers, -- Felipe.
Open this post in threaded view
|

## Concatenating lists

 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