简体   繁体   English

无法将预期的类型“ IO a0”与实际类型“ a1-> IO()”匹配

[英]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是一个列表,您实际上是在组成功能putStrLnunlinesmap 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.

相关问题 无法将预期类型[a0]与实际类型IO()匹配 - couldn't match expected type [a0] with actual type IO () 无法将预期的类型“ IO()”与实际类型“ a0-> m0 a0”匹配 - Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0' Haskell IO:无法将预期类型“IO a0”与实际类型匹配 - Haskell IO: Couldn't match expected type `IO a0' with actual type 有人可以解释此类型错误“无法将期望的类型'IO()'与实际类型'a0-> c0'匹配” - could someone explain this type error “Couldn't match expected type `IO ()' with actual type `a0 -> c0' ” 无法将预期的类型“ IO b0”与实际类型“ [a0]”匹配 - Couldn't match expected type `IO b0' with actual type `[a0]' Haskell中的软件事务存储器:无法将预期的STM a0类型与实际的IO类型进行匹配() - Software Transaction Memory in Haskell: Couldn't match expected type STM a0 with actual type IO () Haskell错误:无法将预期类型'ServerPartT IO a0'与实际类型'[Response]'相匹配 - Haskell error: couldn't match expected type ‘ServerPartT IO a0’ with actual type '[Response]' 无法将类型“[]”与“IO”匹配 预期:IO (IO ()) 实际:[IO ()] - Couldn't match type `[]' with `IO' Expected: IO (IO ()) Actual: [IO ()] 无法将预期类型 `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int -> Int' 与实际类型 `Int' 匹配 - Couldn't match expected type `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int -> Int' with actual type `Int' 无法匹配期望类型'Control.Monad.Trans.Reader.ReaderT MongoContext IO a0'与实际类型'IO()' - Couldn't match expected type 'Control.Monad.Trans.Reader.ReaderT MongoContext IO a0' with actual type 'IO ()'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM