The putStrLn function does not accept the [Char] parameter

Below is the code:

Prelude> :t putStrLn putStrLn :: String -> IO () Prelude> putStrLn "test" test it :: () Prelude> putStrLn "test" ++ "test" <interactive>:25:1: Couldn't match expected type '[Char]' with actual type 'IO ()' In the first argument of '(++)', namely 'putStrLn "test"' In the expression: putStrLn "test" ++ "test" Prelude> :t "test" "test" :: [Char] Prelude> :t "test" ++ "test" "test" ++ "test" :: [Char] Prelude> 

I do not understand the error:

 " Couldn't match expected type '[Char]' with actual type 'IO ()' In the first argument of '(++)', namely 'putStrLn "test"' In the expression: putStrLn "test" ++ "test"" 

How putStrLn takes type [Char] as the first parameter (I assume it is implicitly converted to String?). But this does not set the test if "test" ++ "test" is passed as a parameter, although "test" ++ "test" is of type [Char] as well?

+4
source share
1 answer

Your code does not pass "test" ++ "test" as a parameter. A functional application is bound more strongly than any infix operator. Your code is parsed as

 (putStrLn "test") ++ "test" 

The error you get relates to the fact that putStrLn does not return a string.

To solve this problem, write

 putStrLn ("test" ++ "test") 

Note: String defined as type String = [Char] , i.e. it's just another name for the same type.

+11
source

All Articles