From Learn You a Haskell:
Think about this list:
[5]. That’s just syntactic sugar for5:[]. On
the left side of the:, there’s a value; on the right side, there’s a
list. In this case, it’s an empty list. Now how about the list[4,5]?
Well, that desugars to4:(5:[]). Looking at the first :, we see that
it also has an element on its left side and a list,(5:[]), on its
right side.
The same goes for a list like3:(4:(5:6:[])), which could be written
either like that or like3:4:5:6:[](because:is right-associative)
or[3,4,5,6].
For the bolded part, I was expecting the growing list to culminate in 3:(4:(5:(6:[]))). This has something to do with my lack of understanding of currying, associativity, or both. Can someone tell me the flaw in my thinking?
Multiplication is associative. This means that
(x * y) * zis the same asx * (y * z). However,:is not associative.However, the terms “left-associative” and “right-associative” are different, and unrelated to the term “associative”.
If
*is left-associative, thenx * y * zis the same thing as(x * y) * z. The parentheses are redundant.If
*is right-associative, thenx * y * zis the same thing asx * (y * z). The parentheses are redundant.Currying has nothing to do with this.
Since
:is right-associative,[3,4,5,6]can be written as: