[英]Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO ()’
Works: 作品:
enumerate = \todos ->
setSGR [SetColor Foreground Vivid Red] >>=
(\_ -> putStrLn $ unlines $ map transform todos) >>
setSGR [Reset]
Doesn't work: 不起作用:
enumerate = \todos ->
setSGR [SetColor Foreground Vivid Red] >>
putStrLn . unlines . map transform todos >>
setSGR [Reset]
As far as I can undestand, >>=
passes on the variable, which is then ignored in the subsequent lambda (\\_ -> ...)
. 据我所知,
>>=
传递了变量,然后在随后的lambda (\\_ -> ...)
_- (\\_ -> ...)
中将其忽略。 However, when I transform that to using >>
and have function composition, it doesn't appear to work. 但是,当我将其转换为使用
>>
并具有函数组成时,它似乎不起作用。
What is the difference between the two that causes the second not to compile? 两者之间导致第二个不编译的区别是什么? It'd be great to know why those two expressions aren't equivalent.
很高兴知道为什么这两个表达式不相等。
/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’
This will work: 这将起作用:
enumerate = \todos ->
setSGR [] >>
(putStrLn . unlines . map transform) todos >>
setSGR []
Note that f . g . map h xs
注意
f . g . map h xs
f . g . map h xs
f . g . map h xs
implies that map h xs
is a function you are composing with g
and then f
. f . g . map h xs
表示map h xs
是您要先用g
然后是f
组成的f
。 But map transform todos
is a list, and you are really composing the function putStrLn
, unlines
and map transform
and then applying the composition to the list todos
. 但是
map transform todos
是一个列表,您实际上是在组成功能putStrLn
, unlines
和map transform
,然后将合成应用于列表todos
。
In general: 一般来说:
f $ g $ h x = (f . g . h) x
so your working expression: 所以你的工作表达:
putStrLn $ unlines $ map transform todos
is the same as: 是相同的:
( putStrLn . unlines . map transform ) todos
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.