我对Haskell类型系统有一些麻烦。Haskell读取文件类型问题
现状:
- 下面的程序走的文件名列表上CMDLINE
- 对于所有的文件名读取使用功能READFILE每个文件的
- 内容传递给inputParser其内容(秒差距)
- 作息不那么重要
- 主要问题是功能read_modules
- 前两日'do'表达式在Haskell类型系统中是无效的
- 问题在[String] x IO String x [Char] x ...
- 之间有冲突,但是当函数'parse'它想IO字符串突然(在相同的参数),否则要字符串
我想要什么:
- 阅读每个文件的内容
- 传递内容的“解析”功能第三个参数
这是代码。
module Main where
import System.IO
import System.Environment
import Text.ParserCombinators.Parsec
import InputParser
import Data
usage :: IO()
usage = putStrLn "Usage: x file file file option"
parse_modules :: String -> [Char] -> Either ParseError [Module]
parse_modules filename input = parse inputParser filename input
read_modules :: [String] -> [Module]
read_modules [] = []::[Module]
read_modules (filename:rest) =
do
content <- readFile filename -- HERE is the problem
modules <- case parse_modules filename content of -- HERE is problem too
Left error -> do
putStr "parse error at "
print error
Right out -> out ++ (read_modules rest)
return modules
use :: [String] -> IO()
use args =
do
init <- last args
filenames <- take (length args - 1) args
modules <- read_modules filenames
return()
main :: IO()
main = do args <- getArgs
if length args < 2
then usage
else use args
这里是GHC输出错误
ghc --make -o x.hs input-parser.hs data.hs
[3 of 3] Compiling Main (x.hs, x.o)
x.hs:19:4:
Couldn't match expected type `IO String'
against inferred type `[String]'
In a stmt of a 'do' expression: content <- readFile filename
In the expression:
do content <- readFile filename
modules <- case parse_modules filename content of {
Left error -> do ...
Right out -> out ++ (read_modules rest) }
return modules
In the definition of `read_modules':
read_modules (filename : rest)
= do content <- readFile filename
modules <- case parse_modules filename content of {
Left error -> ...
Right out -> out ++ (read_modules rest) }
return modules
-- THIS ERROR is somewhat not important
x.hs:30:4:
Couldn't match expected type `[Char]'
against inferred type `IO Char'
Expected type: String
Inferred type: IO Char
In a stmt of a 'do' expression: init <- last args
In the expression:
do init <- last args
filenames <- take (length args - 1) args
modules <- read_modules filenames
return()
make: *** [x] Error 1
是什么问题:
- 我不明白我应该通过地方。我有点知道我想要什么,但我没有得到语法或风格。
- 我哈斯克尔greenie(此处未解决)
- 类型
什么问题:
- 如何解决提出的问题类型?我应该把什么放入'解析'功能? 'readFile'给了我什么?这两种类型是兼容的吗?是否需要进行某种类型的转换?
相关网站链接:
- http://book.realworldhaskell.org/read/using-parsec.html
- http://www.zvon.org/other/haskell/Outputprelude/readFile_f.html
- http://www.haskell.org/pipermail/haskell/2002-November/010748.html
谢谢大家的提示和评论。
你的回答并不完全是我想听到的,但是当我阅读了关于monads和< - 运算符的更多信息之后,我的确了解了我的代码中的许多缺陷。谢谢你指出真正的问题。 – Martin 2009-12-21 22:15:21
当涉及IO中的简单函数时,如果你认为'<-'是“实现它”,那么它可能会有帮助(现在)。类型为'IO [String]'的东西表示“做一些I/O并接收一个字符串列表”的想法。一旦你用'<-'提取了值,你已经完成了实际的输入,现在你已经有了你的列表,所以除非你想获得更多的输入,否则你不应该使用另一个'<-'。 – 2009-12-22 00:52:16
虽然,作为对我以前的评论的一个警告,这绝对是过于简单化,所以不要太字面。 – 2009-12-22 00:55:23