简体   繁体   English

putStrLn函数不接受[Char]参数

[英]putStrLn function does not accept [Char] parameter

Below 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 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""

As putStrLn accepts a [Char] type as first parameter (I assume its implicitly converted to String ?). 由于putStrLn接受[Char]类型作为第一个参数(我假设它隐式转换为String?)。 But this does not type check if "test" ++ "test" is passed as parameter, even though "test" ++ "test" is of type [Char] also ? 但是这不会检查"test" ++ "test"是否作为参数传递,即使"test" ++ "test"也是[Char]的类型?

Your code doesn't pass "test" ++ "test" as a parameter. 您的代码不会将"test" ++ "test"作为参数传递。 Function application binds tighter than any infix operator. 函数应用程序比任何中缀运算符都更紧密。 Your code is parsed as 您的代码被解析为

(putStrLn "test") ++ "test"

The error you get refers to the fact that putStrLn does not return a string. 您得到的错误是指putStrLn不返回字符串的事实。

To solve this, write 要解决这个问题,请写信

putStrLn ("test" ++ "test")

Note: String is defined as type String = [Char] , ie it's just a different name for the same type. 注意: String定义为type String = [Char] ,即它只是同一类型的不同名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM