gpt4 book ai didi

Haskell IO 返回无法匹配类型 ‘[Char]’ 与 ‘Char’ 预期类型 : String Actual type: [String]

转载 作者:行者123 更新时间:2023-12-04 10:06:51 24 4
gpt4 key购买 nike

嗨,我正在尝试创建一个用户界面,用户可以在其中选择 mainMenu 来测试该功能。


data Place = Place String Coord [Int]
deriving (Ord,Eq,Show,Read)

data Coord = Cord Double Double
deriving (Ord ,Eq ,Show ,Read)


placeName :: [Place] -> [String]
placeName [] = [""]
placeName plcs = [s | (Place s x(n:ns))<-plcs]




main :: IO ()
main = do putStrLn "Rainfall Map Without A Map"
placeFile <- readFile "place.txt"
let placeData = read placeFile
putStrLn ("\nFile Loaded "++ show(length placeData)++ "places")
mainMenu placeData

mainMenu :: [Place] -> IO()
mainMenu plcs = do
putStrLn(" 1. return the name of all places")
putStrLn(" 2. Display the average rain figures of a place")
putStrLn(" 3. Display places with formatted String")
putStrLn(" 4. Return the place that have zero raingfall")
putStrLn(" 5. Replace a place with new place")
putStrLn(" 6. Save and Exit")
putStr("Please insert a number: ")
choose<- getLine
numberChoose choose plcs

numberChoose :: String -> [Place]->IO()
numberChoose "1" plcs = first plcs
numberChoose "2" plcs = second plcs
numberChoose "3" plcs = third plcs
numberChoose "4" plcs = fourth plcs
numberChoose "5" plcs = fifth plcs
numberChoose "6" plcs = sixth plcs
numberChoose _ plcs = do
putStrLn ("Error!:-- Wrong Input")
mainMenu plcs


first :: [Place]->IO()
first plcs = do
putStrLn $ placeName plcs
mainMenu plcs

所以我的 placeName 函数仅用于从 Place 返回字符串列表,而我的第一个函数仅用于在用户插入相应的 num 时返回字符串列表。我假设我的 mainMenu 显示的数据类型与我的第一个函数相同。它给了我错误:
    • Couldn't match type ‘[Char]’ with ‘Char’
Expected type: String
Actual type: [String]
• In the second argument of ‘($)’, namely ‘placeName plcs’
In a stmt of a 'do' block: putStrLn $ placeName plcs
In the expression:
do putStrLn $ placeName plcs
mainMenu plcs
|
153 | putStrLn $ placeName plcs | ^^^^^^^^^^^^^^

最佳答案

putStrLn有类型 putStrLn :: String -> IO () ,所以它打印一个字符串,而不是一个字符串列表。

您可以使用 mapM_ :: (Foldable f, Monad m) -> (a -> m b) -> f a -> m () 对每个元素执行一元函数,所以:

first :: [Place] -> IO ()
first plcs = do
mapM_ putStrLn (placeName plcs)
mainMenu plcs

或者您可以使用 unlines :: [String] -> String 将字符串列表转换为由新行分隔的字符串:
first :: [Place] -> IO ()
first plcs = do
putStrLn (unlines (placeName plcs))
mainMenu plcs

关于Haskell IO 返回无法匹配类型 ‘[Char]’ 与 ‘Char’ 预期类型 : String Actual type: [String],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61543111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com