简体   繁体   English

Haskell:无法将预期类型“IO t0”与实际类型“[Char]”匹配

[英]Haskell: Couldn't match expected type ‘IO t0’ with actual type ‘[Char]’

myTakeWhile :: (a-> Bool ) -> [a] -> [a]
myTakeWhile _ [] = []
myTakeWhile pred (head:tail) = if pred head
                           then head:myTakeWhile pred tail
                           else []

main =  myTakeWhile (/= ' ') "This is practice."

gives me this error:给我这个错误:

Couldn't match expected type ‘IO t0’ with actual type ‘[Char]’
    • In the expression: main
      When checking the type of the IO action ‘main’

A main always has type IO something , not a String which is the result type myTakeWhile (/= ' ') "This is practice." main始终具有类型IO something ,而不是结果类型为myTakeWhile (/= ' ') "This is practice."String . .

You can work with print:: Show a => a -> IO () to print it as a string literal , or putStrLn:: String -> IO () to print the content of the string, so:您可以使用print:: Show a => a -> IO ()将其打印为字符串文字,或putStrLn:: String -> IO ()打印字符串的内容,因此:

main :: IO ()
main =  print (myTakeWhile (/= ' ') "This is practice.")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法匹配预期类型`[([Char],a0)]'与实际类型`([Char],t0)'Haskell - Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' Haskell Haskell:无法将期望的类型“ IO t0”与实际类型“ Integer”匹配 - Haskell: Couldn't match expected type 'IO t0' with actual type 'Integer' Haskell powerset函数-如何避免无法将期望的类型“ IO t0”与实际类型“ [[Integer]]”匹配 - Haskell powerset function - How to avoid Couldn't match expected type `IO t0' with actual type `[[Integer]]' Haskell无法将预期类型[char]与实际类型IO匹配 - Haskell couldn't match expected type [char] with actual type IO 无法将预期类型`Maybe(String,Int,String)'与实际类型`([Char],t0,[Char])'匹配 - Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])' Haskell IO 返回无法将类型“[Char]”与“Char”匹配预期类型:字符串实际类型:[字符串] - Haskell IO return Couldn't match type ‘[Char]’ with ‘Char’ Expected type: String Actual type: [String] Haskell无法将预期类型'String'与实际类型'Char'匹配 - Haskell Couldn't match expected type 'String' with actual type 'Char' Haskell类型错误“无法将类型“ Char”与“ t0 [Char]”匹配” - Haskell type error “Couldn't match type `Char' with `t0 [Char]'” 无法将预期类型“(t1,t2,t0)”与实际类型“ [a0]”匹配 - Couldn't match expected type `(t1, t2, t0)' with actual type `[a0]` 无法将预期类型(Int - > Int - > Int)与实际类型`(t0,t1,t2)'匹配 - couldn't match expected type (Int -> Int -> Int) with actual type `(t0, t1, t2)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM