I'm trying to output a bunch of cities in a nice text format, each one starting on a new line. With each piece of data comma separated. But I'm getting the error, cannot match expected type Char to actual type [[Char]]. For example
("New York", (1,1), [5, 4, 3, 2])
would be outputted as:
New York, (1,1), [5, 4, 3, 2]
Here's my code for it so far:
type Name = String
type Coordinates = (Int, Int)
type Pop = Int
type TotalPop = [Pop]
type Place = (Name, Coordinates, TotalPop)
places :: [Place]
places = [("New York", (1,1), [5, 4, 3, 2]),
("Washington DC", (1, 1), [4, 3, 2, 1]),
("Los Angeles", (3,3), [7, 6, 5, 4])]
placesToString :: [Place] -> IO ()
placesToString ps = putStrLn (showPlaces ps)
showPlaces :: [Place] -> String
showPlaces ps = [showPlace p ++ "\n" | p <- ps] where
showPlace p = [showData d | d <- p] where
showData (w, (x,y), z) = w ++ ", (" ++ show x ++ ", " ++ show y ++ "), [" ++ unwords (map show z) ++ "]"
The full error is as shown:
template.hs:14:27: error:
• Couldn't match type ‘(Name, Coordinates, TotalPop)’
with ‘[([Char], (a0, a1), [a2])]’
Expected type: [([Char], (a0, a1), [a2])]
Actual type: Place
• In the first argument of ‘showPlace’, namely ‘p’
In the first argument of ‘(++)’, namely ‘showPlace p’
In the expression: showPlace p ++ "\n"
|
14 | showPlaces ps = [showPlace p ++ "\n" | p <- ps] where
| ^
template.hs:14:32: error:
• Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [[Char]]
Actual type: [Char]
• In the second argument of ‘(++)’, namely ‘"\n"’
In the expression: showPlace p ++ "\n"
In the expression: [showPlace p ++ "\n" | p <- ps]
|
14 | showPlaces ps = [showPlace p ++ "\n" | p <- ps] where
|
Thanks in advance for any help given :)
showCities?concatjust after both your list comprehensions, e.g.... = concat [showPlace p ++ "\n" | p <- ps]. You need to convert your list-of-strings into a single string.