I need to add my own data type to a list which is in a function, here’s my code:
type Car = (String, String, Int, String)
testDatabase :: [Car]
testDatabase = [("Ford", "Petrol", 2006, "Sport")]
addNewCar :: Car
addNewCar newCar = newCar:testDatabase
Here’s the error I get:
ERROR file:.\template.hs:20 - Type error in explicitly typed binding
*** Term : addNewCar
*** Type : ([Char],[Char],Int,[Char]) -> [([Char],[Char],Int,[Char])]
*** Does not match : Car
(sorry its a rubbish explanation im just struggling a tad with Haskell). Thank you in advance!!
Ash!
The inferred type of
addNewCaris([Char],[Char],Int,[Char]) -> [([Char],[Char],Int,[Char]), which is the same asCar -> [Car]. This type says thataddNewCaris a function which takes a car and returns a list of cars. This is exactly the type you want.However your type signature says that
addNewCaris simply a value of typeCar. This is wrong and clashes with the inferred type. That’s why you get the error. So to fix this, simply remove the type signature or change it toaddNewCar :: Car -> [Car].