I'm currently learning Haskell, and working with List
.
According to the HaskellWiki, if I wanted to merge two lists together, I would write:
list1 ++ list2
However, according to this answer, using ++ on a large list would be inefficient.
From researching, I came across this SO page, but that question requires a specific requirement for the output of the List
.
What I tried:
Suppose if I had two lists of numbers (for this example, assume that both lists are large enough that using ++
would be inefficient as described in the SO answer):
oldNumbers = [1,5,14,22,37]
newNumbers = [3,10,17,27,34,69]
allNumbers = oldNumbers:newNumbers
As you can see I attempted to add oldNumbers
to the head of newNumbers
with the intention of reversing it afterward (disregard that allNumbers
would be unorder for now, that's an exercise for another day).
As you probably guessed, it produced an error
error:
* Non type-variable argument in the constraint: Num [a]
(Use FlexibleContexts to permit this)
* When checking the inferred type
allNumbers :: forall a. (Num a, Num [a]) => [[a]]
So, as stated in the title, how would I efficiently merge two lists?