无法弄清楚如何使下面的代码正常工作。我尝试了所有可能的typeigs。
代码
type SerialNumber = (String, Int)
serialList :: Map.Map String SerialNumber
serialList = Map.fromList [("belt drive",("BD",0001))
,("chain drive",("CD",0002))
,("drive pulley",("DP",0003))
,("drive sprocket",("DS",0004))
]
findSerial :: Ord k => k -> Map.Map k a -> Maybe a
findSerial input = Map.lookup input
outOfContext (Just (a, b)) = (a, b)
getSerialFromUser :: IO ()
getSerialFromUser = do
putStr "Lookup part: "
input <- getLine
let output = findSerial input serialList
putStrLn "\nFound entry: " ++ output
错误
• Couldn't match expected type ‘[()]’
with actual type ‘Maybe SerialNumber’
• In the second argument of ‘(++)’, namely ‘output’
In a stmt of a 'do' block: putStrLn "\nFound entry: " ++ output
In the expression:
do putStr "Lookup part: "
input <- getLine
let output = findSerial input serialList
putStrLn "\nFound entry: " ++ output
|
62 | putStrLn "\nFound entry: " ++ output
在没有书籍的情况下尝试我在Haskell的第一步,请保持温柔。
答案 0 :(得分:3)
在Haskell函数中,应用程序保持关联,表示表达式:
putStrLn "\nFound entry: " ++ output
解析为
(putStrLn "\nFound entry: ") ++ output
但是您可能希望将其解析为:
putStrLn ("\nFound entry: " ++ output)
这就是为什么您需要显式指定括号或使用$
运算符的原因:
putStrLn $ "\nFound entry: " ++ output
但是请注意,output
是SerialNumber
类型的,但是(++)
需要两个列表作为参数。因此,您需要修改要为程序指定的行为。