【发布时间】:2013-12-24 05:33:53
【问题描述】:
以下代码有什么问题?我只是想在文件中转换以下格式的输入: n - 测试用例数 // n 个数字 n1 n2 (通过标准输入读取)到整数列表中并显示它?
socks :: Int -> Int
socks x = x + 1
strToInt = read :: String -> Int
strLToIntL :: [String] -> [Int]
strLToIntL xs = map (strToInt) xs
main = do
n <- readLn :: IO Int
mapM_ putStrLn $ map show $ strLToIntL $ fmap (take n . lines) getContents
运行时出现编译错误:
Couldn't match expected type `Char' with actual type `[Char]'
Expected type: String -> [Char]
Actual type: String -> [String]
In the second argument of `(.)', namely `lines'
In the first argument of `fmap', namely `(take n . lines)'
【问题讨论】:
-
为什么不打破那长长的队伍呢?我认为如果您使用多个表达式并为每个分配添加类型签名,您会很快发现问题。找到问题后,您可以将其简化为更紧凑的形式。
标签: haskell