Tôi muốn viết các hàm và đặt kết quả vào chuỗi.Haskell IO (Chuỗi) và Chuỗi
Tôi muốn chức năng:
read' :: FilePath -> String
tôi sử dụng:
:t readFile
readFile :: FilePath -> IO String
tôi thực hiện:
read' :: IO()
read' = do
str <- readFile "/home/shk/workspace/src/test.txt"
putStrLn str
Tôi muốn hỏi str là chuỗi hay không?
Chúng ta biết rằng:
:t putStrLn
putStrLn :: String -> IO()
Thế thì tại sao tôi không thể:
read' :: String
read' = do
str <- readFile "/home/shk/workspace/lxmpp/src/test.txt"
str
tôi nhận được lỗi:
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 }
Cảm ơn bạn.
ReadFile trong ký hiệu có nghĩa là bạn đang ở trong đơn IO, và đơn nguyên IO không thể thoát được! – is7s
@ is7s trừ khi bạn sử dụng 'unsafePerformIO'! – alternative
Quy tắc đầu tiên của unsafePerformIO là bạn không nói cho ai biết về unsafePerformIO! –