Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何使用“读取和处理文件”;是否使用“符号”;?_Haskell - Fatal编程技术网

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
             ...