0

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.

2
  • 2
    SInce x is a list, you can not here use x : ..., unless you again want to create a list of lists. Commented Oct 23, 2019 at 12:04
  • Do you mind posting the type error for future searchability? Commented Oct 25, 2019 at 1:00

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

hello you have just to replace your x : myConcat xs by x ++ myConcat xs

2 Comments

That's not enough. The pattern matches remain incomplete.
yes sure the case of empty List! My solution isnt complete, i just wanted to highlight the difference between : and ++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.