在涉及 ReadMode 的函数应用程序中意外执行块此消息是什么意思?
Unexpected do block in function application involving ReadMode what does mean this message?
此函数读取一个字符串并连接到一个 txt 文件
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile ("listagemCategorias.txt") ReadMode
categorias <- hGetContents file
let categoria1 = (read categorias :: [Char]) ++ categoria
hClose file
file2 <- openFile ("listagemCategorias.txt") WriteMode
hPutStr file2 (categoria)
hFlush file2
hClose file2
但我收到此错误消息:
main.hs:190:41: error:
Unexpected do block in function application:
do file <- openFile ("listagemCategorias.txt") ReadMode
You could write it with parentheses
Or perhaps you meant to enable BlockArguments?
|
190 | adicionaEmListaDeCategorias categoria = do
| ^^...
正如我在评论中所说 - 问题是您打算在第一行使用 tab
这个有效:
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile "listagemCategorias.txt" ReadMode
categorias <- hGetContents file
let categoria1 = (read categorias :: [Char]) ++ categoria
hClose file
file2 <- openFile "listagemCategorias.txt" WriteMode
hPutStr file2 categoria
hFlush file2
hClose file2
但是 Haskell 会给你一个警告,因为你没有使用 categoria1
我想你会想要这个:
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile "listagemCategorias.txt" ReadMode
categorias <- hGetContents file
let categoria1 = (read categorias :: [Char]) ++ categoria
hClose file
file2 <- openFile "listagemCategorias.txt" WriteMode
hPutStr file2 categoria1
hFlush file2
hClose file2
(写出拼接版)
同样根据你的问题描述,我认为你的版本不会达到你的预期(read
会失败,你不需要它):
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile "listagemCategorias.txt" ReadMode
fileContent <- hGetContents file
hClose file
let newFileContent = fileContent ++ categoria
file2 <- openFile "listagemCategorias.txt" WriteMode
hPutStr file2 newFileContent
hFlush file2
hClose file2
此函数读取一个字符串并连接到一个 txt 文件
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile ("listagemCategorias.txt") ReadMode
categorias <- hGetContents file
let categoria1 = (read categorias :: [Char]) ++ categoria
hClose file
file2 <- openFile ("listagemCategorias.txt") WriteMode
hPutStr file2 (categoria)
hFlush file2
hClose file2
但我收到此错误消息:
main.hs:190:41: error:
Unexpected do block in function application:
do file <- openFile ("listagemCategorias.txt") ReadMode
You could write it with parentheses
Or perhaps you meant to enable BlockArguments?
|
190 | adicionaEmListaDeCategorias categoria = do
| ^^...
正如我在评论中所说 - 问题是您打算在第一行使用 tab
这个有效:
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile "listagemCategorias.txt" ReadMode
categorias <- hGetContents file
let categoria1 = (read categorias :: [Char]) ++ categoria
hClose file
file2 <- openFile "listagemCategorias.txt" WriteMode
hPutStr file2 categoria
hFlush file2
hClose file2
但是 Haskell 会给你一个警告,因为你没有使用 categoria1
我想你会想要这个:
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile "listagemCategorias.txt" ReadMode
categorias <- hGetContents file
let categoria1 = (read categorias :: [Char]) ++ categoria
hClose file
file2 <- openFile "listagemCategorias.txt" WriteMode
hPutStr file2 categoria1
hFlush file2
hClose file2
(写出拼接版)
同样根据你的问题描述,我认为你的版本不会达到你的预期(read
会失败,你不需要它):
adicionaEmListaDeCategorias :: [Char] -> IO()
adicionaEmListaDeCategorias categoria = do
file <- openFile "listagemCategorias.txt" ReadMode
fileContent <- hGetContents file
hClose file
let newFileContent = fileContent ++ categoria
file2 <- openFile "listagemCategorias.txt" WriteMode
hPutStr file2 newFileContent
hFlush file2
hClose file2