Reputation: 59
Im practicing recursion on Haskell and need to do concat method with own impelentation on a nested list.
I've tried
myConcat :: [[a]] -> [a]
myConcat [[]] = []
myConcat ((x:xs)) = x : myConcat ((xs))
But it doesnt seem to work.
myConcat [[1,2],[3,4,5],[6]] == [1,2,3,4,5,6]
This is the goal.
The main problem is that i dont really know how to work with nested lists.
Upvotes: 0
Views: 2159
Reputation: 505
hello you have just to replace your x : myConcat xs
by x ++ myConcat xs
Upvotes: 1
Reputation: 476719
Since x
is a list here (it has type [a]
), you can not use x : …
here, unless you again want to create a list of lists, but then your myConcat
would act like an id
function for lists.
You here need to append x
with the rest of the list, so you can use (++) :: [a] -> [a] -> [a]
, indeed:
myConcat :: [[a]] -> [a]
myConcat [] = []
myConcat (x:xs) = x ++ myConcat xs
Note that you forgot the base case []
. By writing [[]]
you match a list with one element: the empty list. That is something different.
Upvotes: 5