haskell 在函数中将用户输入更改为用户定义的数据类型时出错
haskell error change user input to user defined data type in function
handleStatements :: [Term] -> IO ()
handleStatements statements = do
let (queries, clauses) = partition isQuery statements
mapM_ (clausesEntailProof clauses) queries
--apply clauses to queries and ignore result
handleArgs :: String-> IO ()
handleArgs args = do
contents <- readFile $ args
case parseInput contents of
Left err -> print err
Right statements -> handleStatements statements
main :: IO ()
main = do
handleStatements(input)
where input = getLine >>= (\str -> ((readIO str)::IO[Term]))
我出错了。
无法将预期类型“[Term]”与实际类型“IO Term”匹配。我该如何解决这个问题?
如果你在where
子句中定义了input = …
,那么input
的类型是IO [Term]
,而不是[Term]
,所以你不能传递input
到 handleStatement
s.
你可以这样写:
main :: IO ()
main = do
<b>input</b> <- getLine >>= (\str -> ((readIO str)::IO[Term]))
handleStatements <b>input</b>
但是你把事情搞得太复杂了,你可以用 readLn :: Read a => IO a
代替:
main :: IO ()
main = do
input <- <b>readLn</b>
handleStatements input
或者用 (>>=) :: Monad m => m a -> (a -> m b) -> m b
function:
替换 do 块
main :: IO ()
main = readLn <b>>>=</b> handleStatements
handleStatements :: [Term] -> IO ()
handleStatements statements = do
let (queries, clauses) = partition isQuery statements
mapM_ (clausesEntailProof clauses) queries
--apply clauses to queries and ignore result
handleArgs :: String-> IO ()
handleArgs args = do
contents <- readFile $ args
case parseInput contents of
Left err -> print err
Right statements -> handleStatements statements
main :: IO ()
main = do
handleStatements(input)
where input = getLine >>= (\str -> ((readIO str)::IO[Term]))
我出错了。 无法将预期类型“[Term]”与实际类型“IO Term”匹配。我该如何解决这个问题?
如果你在where
子句中定义了input = …
,那么input
的类型是IO [Term]
,而不是[Term]
,所以你不能传递input
到 handleStatement
s.
你可以这样写:
main :: IO ()
main = do
<b>input</b> <- getLine >>= (\str -> ((readIO str)::IO[Term]))
handleStatements <b>input</b>
但是你把事情搞得太复杂了,你可以用 readLn :: Read a => IO a
代替:
main :: IO ()
main = do
input <- <b>readLn</b>
handleStatements input
或者用 (>>=) :: Monad m => m a -> (a -> m b) -> m b
function:
main :: IO ()
main = readLn <b>>>=</b> handleStatements