Haskell use instanced Read in main with Type

68 views Asked by At

How is it possible to use a instanced read in my main?

Right now i have the following:

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    

This will compile/interprete but when i call my main and enter"2x^2" it just returns "2x^2" instead of Monom(2.0,2). If you call (read "2x^2")::Term it will work fine, just how do i do it in my main?

Normaly i would just return

(read inpStr)::Term  

instead of putStrLn because read implements show but the interpreter is making me crazy... Any Help would be appriciated

1

There are 1 answers

0
Tomo On BEST ANSWER

You can use type qualifier in let. Your main would look like this:

main :: IO ()
main = do
   putStrLn "Insert a Term:"
   inpStr <- getLine
   let outStr = read inpStr :: Term
   putStrLn $ show outStr