在建议我收到here,我试图重写一个函数没有多余的绑定分配和返回,但是被一个额外的IO阻塞我似乎无法理解如何摆脱它。Haskell函数重写没有绑定返回
我
good :: IO (Either Int String)
getit :: Either Int String -> Int
main :: IO()
main = do
x <- fmap getit good
putStrLn $ show x
主要工作正常。但是....
main2 :: IO()
main2 = do
putStrLn $ show $ fmap getit good
-- let's try totally without do
main3 :: IO()
main3 = putStrLn $ fmap show $ fmap getit good
MAIN2失败:
• No instance for (Show (IO Int)) arising from a use of ‘show’
• In the second argument of ‘($)’, namely ‘show $ fmap getit good’
In a stmt of a 'do' block: putStrLn $ show $ fmap getit good
In the expression: do { putStrLn $ show $ fmap getit good }
而且main3失败:
• Couldn't match type ‘IO’ with ‘[]’
Expected type: String
Actual type: IO String
什么是习惯用法改写这个正确的方法是什么?
(子问题:是“< - ”这家伙居然叫绑定通过这里:Are there pronounceable names for common Haskell operators?)
而且'print = putStrLn。 show',所以'main = good >> = print。 getit' – freestyle