如何在 Haskell 中转换 [[Char]] -> Char?
How can I convert [[Char]] -> Char in Haskell?
我正在尝试以一种漂亮的文本格式输出一堆城市,每个城市都在一个新行开始。每条数据用逗号隔开。但是我得到了错误,cannot match expected type Char to actual type [[Char]]
。例如
("New York", (1,1), [5, 4, 3, 2])
将输出为:
New York, (1,1), [5, 4, 3, 2]
到目前为止,这是我的代码:
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) ++ "]"
完整错误如图:
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
|
提前感谢您提供的任何帮助:)
我使用 intercalate
from Data.List 使其工作(插入类似于其他语言中的 Array.join)
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) ++ "]"
我正在尝试以一种漂亮的文本格式输出一堆城市,每个城市都在一个新行开始。每条数据用逗号隔开。但是我得到了错误,cannot match expected type Char to actual type [[Char]]
。例如
("New York", (1,1), [5, 4, 3, 2])
将输出为:
New York, (1,1), [5, 4, 3, 2]
到目前为止,这是我的代码:
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) ++ "]"
完整错误如图:
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
|
提前感谢您提供的任何帮助:)
我使用 intercalate
from Data.List 使其工作(插入类似于其他语言中的 Array.join)
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) ++ "]"