我试图从文件中获取一些数据,然后解析它并将它作为参数传递给另一个函数。Haskell - 读取文件并将数据作为参数传递给函数
data LogLine = LogLine {
name :: String
, args1 :: String
, args2 :: String
, constant :: String
} deriving (Ord, Show, Eq)
main = do
file <- readFile "foo"
let result = (parse final "Input" file) --Parses the file into the LogLine datatype
let firstargs = getFirstArgs result --Get the first argument out of the datatype
let secondargs = getSecondArgs result --Get the second argument out of the datatype
let constant = getConstant result --Get the constant out of the datatype
createGraph firstargs secondargs constant --THIS IS THE PROBLEM
的问题是,每当我试图读取一个文件就变成一个(IO字符串),我总是携带IO无论我做什么。 的createGraph
函数声明为
createGraph :: String -> String -> String -> Argument
但每当我试图执行的最后一条语句,报告说:
Couldn't match expected type `IO a0' with actual type `Argument'
In the return type of a call of `createGraph'
我不能改变createGraph
函数的返回类型,因为它是这是我需要提供参数的一个大框架的一部分。 处理这个问题的方法是什么?
东西是不是你的代码和你的描述之间的匹配。您可以在代码中调用'createGraph'并在您的描述中引用'createArgument'。假设它们是相同的函数,问题是'main'的返回类型必须是'IO()',而'createGraph'的返回类型是'Argument'。 – asm
对不起,这是一个错字。它们都是相同的功能,为了明确起见我想重命名它们。 –
你的代码片段中的main函数是你的程序的主要函数吗?如果是这样的话,do块中的最终表达式必须具有类型'IO()' – asm