user585923
user585923

Reputation: 23

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
   | ^

Upvotes: 2

Views: 100

Answers (2)

Daniel Wagner
Daniel Wagner

Reputation: 152857

flatten [(x,y):xs)] = x:(show y ++ flatten xs)
                 ^

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476709

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

Upvotes: 2

Related Questions