Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' Haskell

char, ghci, haskell, int

Solution

In the line:

total ([("c",e)]:y) = total y ++ [e]

the `([("c",e)]:y)` does not do what you want. It matches a nonempty list in which the first element is also a list (because of the `[...]`) and in which that sublist has exactly one element, which is a pair whose first element equals `"c"`. In order to match what you want, you need to write:

total ((c,e):y) = total y ++ [e]

However, this still won't do what you want, as it constructs a list of all of the `e` values in the input list. To sum them together, you need to do:

total [] = 0
total ((c,e):y) = total y + e

Problem

I am starting to program with haskell. The program I am developing just sums the total of a list with two elementes, for example: ``` [("book",10),("cookies",2),("icecream",5)] ``` This should return "17". Here i my code: ``` total [] = [] total ([("c",e)]:y) = total y ++ [e] ``` But while running in GHCi it gives me this error: ``` <interactive>:80:8: Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' In the expression: ("livro", 10) In the first argument of `total', namely `[("livro", 10), ("bolachas", 2), ("gelado", 5)]' In the expression: total [("livro", 10), ("bolachas", 2), ("gelado", 5)] <interactive>:80:21: Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t1)' In the expression: ("bolachas", 2) In the first argument of `total', namely `[("livro", 10), ("bolachas", 2), ("gelado", 5)]' In the expression: total [("livro", 10), ("bolachas", 2), ("gelado", 5)] <interactive>:80:36: Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t2)' In the expression: ("gelado", 5) In the first argument of `total', namely `[("livro", 10), ("bolachas", 2), ("gelado", 5)]' In the expression: total [("livro", 10), ("bolachas", 2), ("gelado", 5)] ``` This is probably very simple but as a beginner I was not able to solve this.

Original source