I was reading on this Haskell page about adding an element to the end of a List
.
Using the example, I tried it out for my self. Given the following List
I wanted to add the number 56
at the end of it.
Example:
let numbers = [4,8,15,16,23,42]
numbers ++ [56]
I was thrown off by this comment:
Adding an item to the end of a list is a fine exercise, but usually you shouldn't do it in real Haskell programs. It's expensive, and indicates you are building your list in the wrong order. There is usually a better approach.
Researching, I realize that what I'm actually doing is creating a List
with 56
as the only element and I'm combining it with the numbers
list
. Is that correct?
Is using ++
the correct way to add an element to the end of a List
?