在建議我收到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