Haskell 如何使用“读取和处理文件”;是否使用“符号”;?
例如,我想读取一个只包含整数的文件,并对这些整数执行类似于函数中的操作:Haskell 如何使用“读取和处理文件”;是否使用“符号”;?,haskell,Haskell,例如,我想读取一个只包含整数的文件,并对这些整数执行类似于函数中的操作: getIntFromFile :: () -> Int getIntFromFile _ = do h <- openFile "/tmp/file" ReadMode str <- hGetLine h let x = read str :: Int
getIntFromFile :: () -> Int
getIntFromFile _ = do
h <- openFile "/tmp/file" ReadMode
str <- hGetLine h
let x = read str :: Int
str <- hGetLine h
let y = read str :: Int
hClose h
x + y
getIntFromFile::()->Int
getIntFromFile=do
h如果执行IO,则必须在IO单子内工作:
getIntFromFile :: () -> IO Int -- Added IO here
getIntFromFile _ = do
h <- openFile "/tmp/file" ReadMode
str <- hGetLine h
let x = read str :: Int
str <- hGetLine h
let y = read str :: Int
hClose h
return (x + y) -- Added return
事实上,在Haskell中使用()->…
是一种反模式。如果使用IO,则必须在IO monad中工作:
getIntFromFile :: () -> IO Int -- Added IO here
getIntFromFile _ = do
h <- openFile "/tmp/file" ReadMode
str <- hGetLine h
let x = read str :: Int
str <- hGetLine h
let y = read str :: Int
hClose h
return (x + y) -- Added return
事实上,在Haskell中使用()->…
是一种反模式
getIntFromFile :: IO Int -- Added IO here
getIntFromFile = do
...