如何在我的 main 中使用实例化读取?
现在我有以下内容:
data Term = Monom (Float,Int)
| Addition (Term,Term)
| Subtraction (Term,Term)
| Multiplication (Term,Term)
| Division (Term,Term)
deriving(Show)
instance Read Term where
readsPrec _ inp = let[(a,b)] = lex inp in
case a of
"x" -> readsPrec 0 b
"^" -> [(Monom (1.0,(read b::Int)), "")]
c -> let[(d, "")] = readsPrec 0 b in
[(Monom( (read c::Float),((\(Monom(x,y)) -> y) d)), "")]
--Aufruf: (read "2x^2")::Term
main :: IO ()
main = do
putStrLn "Insert a Term:"
inpStr <- getLine
let outStr = (read inpStr)
putStrLn outStr
这将编译/解释,但是当我调用我的 main 并输入“2x^2”时,它只返回“2x^2”而不是 Monom(2.0,2)。如果您调用 (read "2x^2")::Term 它会正常工作,我该怎么做呢?
通常我会回来
(read inpStr)::Term
而不是 putStrLn 因为读取实现显示但解释器让我发疯......任何帮助都会被应用