1

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 :)

10
  • 2
    What's showCities? Commented Apr 22, 2021 at 9:59
  • 1
    The full error text would also help. We shouldn't guess at which line the error occurs. Commented Apr 22, 2021 at 10:45
  • 1
    Maybe you need to concat just 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. Commented Apr 22, 2021 at 10:45
  • 1
    a bit OT (sorry) but just out of curiosity: is this some kind of course-work (there are quite a few questions around this very problem/data floating around here lately ;)) - are you fine with saying which university this course is taking place at? Commented Apr 22, 2021 at 11:16
  • 1
    @Carsten sure is xD it's Peter Symond's College ;p Commented Apr 22, 2021 at 11:18

1 Answer 1

1

I got it working using intercalate from Data.List (intercalate is similar to Array.join in other languages)

import Data.List (intercalate)

showPlaces :: [Place] -> String
showPlaces ps = intercalate "\n" [showData d | d <- ps]
  where
    showData (w, (x, y), z) = w ++ ", (" ++ show x ++ ", " ++ show y ++ "), [" ++ unwords (map show z) ++ "]"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.