Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

parse error in function that is supposed to convert list to string – haskell

so my function ‘flatten’ is to take a list of characters and digits to a string

flatten :: [(Char, Int)] -> String
flatten [] = []
flatten [(x,y):xs)] = x:(show y ++ flatten xs)

but I keep getting parse error, can anyone help me understand? Thanks.

 parse error (possibly incorrect indentation or mismatched brackets)
   |
18 | flatten :: [(Char, Int)] -> String
   | ^

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The compile error likely originates from something before the flatten function, for example you forgot to close a bracket.

As for the flatten function itself, for the second clause you should not use square brackets, otherwise you define a pattern of a list of lists of 2-tuples. You thus use (x, y):xs) as pattern:

flatten :: [(Char, Int)] -> String
flatten [] = []
flatten ((x,y):xs) = x : show y ++ flatten xs
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading