我想寫的功能,並把結果字符串。Haskell的IO(字符串)和字符串
我想功能:
read' :: FilePath -> String
我用:
:t readFile
readFile :: FilePath -> IO String
我做:
read' :: IO()
read' = do
str <- readFile "/home/shk/workspace/src/test.txt"
putStrLn str
我想問str是字符串或不是?
我們知道:
:t putStrLn
putStrLn :: String -> IO()
那麼我爲什麼不能:
read' :: String
read' = do
str <- readFile "/home/shk/workspace/lxmpp/src/test.txt"
str
我得到錯誤:
Couldn't match expected type `[t0]' with actual type `IO String'
In the return type of a call of `readFile'
In a stmt of a 'do' expression:
str <- readFile "/home/shk/workspace/lxmpp/src/test.txt"
In the expression:
do { str <- readFile "/home/shk/workspace/src/test.txt";
str }
謝謝。
do-notation中的readFile意味着你處於IO monad中,並且IO monad不能被轉義! – is7s
@ is7s,除非您使用'unsafePerformIO'! – alternative
UnsafePerformIO的第一條規則是你不告訴任何人有關unsafePerformIO! –