I want to append 3 lists or more at once in a single expression.
a ++ b ++ c
Will the ++ operator will be evaluated from left to right or right to left?
1. (a ++ b) ++ c
2. a ++ (b ++ c)
I would say option 2, because if ++ was a prefix function, we would write ++ a ++ b c which naturally leads to evaluating ++ b c first. I’m not sure if I’m correct.
But if it’s option 1, it seems to me that explicitely changing the order of evaluation from right to left is more efficient:
a ++ (b ++ c)
Here is why: a ++ b ++ c will first evaluate to ab ++ c in n steps (where n is the length of a and ab is of course the concatenation of a and b) and then to abc in n+m more steps (m being the length of b, thus n+m the is the length of ab), which makes a total of 2n+m steps. Whereas a ++ (b ++ c) will first evaluate to a ++ bc in m steps, and then to abc in n more steps, which is a total of n+m steps only.
I’m new to haskell and not sure about what I’m saying, I’d like some confirmation.
is parsed as
for exactly the reasons you describe: had
(++)been left-associative, thena ++ b ++ cwould copyatwice. You can check this in GHCi withwhich will respond with
where
infixrmeans “infix, right-associative”.