[英]Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO ()’
作品:
enumerate = \todos ->
setSGR [SetColor Foreground Vivid Red] >>=
(\_ -> putStrLn $ unlines $ map transform todos) >>
setSGR [Reset]
不起作用:
enumerate = \todos ->
setSGR [SetColor Foreground Vivid Red] >>
putStrLn . unlines . map transform todos >>
setSGR [Reset]
據我所知, >>=
傳遞了變量,然后在隨后的lambda (\\_ -> ...)
_- (\\_ -> ...)
中將其忽略。 但是,當我將其轉換為使用>>
並具有函數組成時,它似乎不起作用。
兩者之間導致第二個不編譯的區別是什么? 很高興知道為什么這兩個表達式不相等。
/Users/atimberlake/Webroot/HTodo/htodo/app/Main.hs:18:25:
Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO ()’
In the second argument of ‘(>>)’, namely
‘putStrLn . unlines . map transform todos’
In the first argument of ‘(>>)’, namely
‘setSGR [SetColor Foreground Vivid Red]
>> putStrLn . unlines . map transform todos’
In the expression:
setSGR [SetColor Foreground Vivid Red]
>> putStrLn . unlines . map transform todos
>> setSGR [Reset]
/Users/atimberlake/Webroot/HTodo/htodo/app/Main.hs:18:46:
Couldn't match expected type ‘a1 -> [String]’
with actual type ‘[[Char]]’
Possible cause: ‘map’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘map transform todos’
In the second argument of ‘(.)’, namely
‘unlines . map transform todos’
這將起作用:
enumerate = \todos ->
setSGR [] >>
(putStrLn . unlines . map transform) todos >>
setSGR []
注意f . g . map h xs
f . g . map h xs
f . g . map h xs
表示map h xs
是您要先用g
然后是f
組成的f
。 但是map transform todos
是一個列表,您實際上是在組成功能putStrLn
, unlines
和map transform
,然后將合成應用於列表todos
。
一般來說:
f $ g $ h x = (f . g . h) x
所以你的工作表達:
putStrLn $ unlines $ map transform todos
是相同的:
( putStrLn . unlines . map transform ) todos
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.