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

850 Views Asked by At
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’
1

There are 1 best solutions below

0
On

A main always has type IO something, not a String which is the result type myTakeWhile (/= ' ') "This is practice.".

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:

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