module Main where
import Numeric
import Monad

main = do contents <- getContents; foldM (evalLine) [] (lines contents)
    where evalLine stack line = report $ foldl rpnEval stack
                                $ map toToken (words line)
          report stack@(h:_)  = do print h; return stack
          report _            = return []
          
data PolyToken a = Operator (a -> a -> a) | Value a
type Token = PolyToken Float

toToken w = case readSigned readFloat w of
            ((value, _):_) -> Value value
            _              -> Operator $ operator w

rpnEval stack (Value value)               = value:stack
rpnEval (second:first:rest) (Operator op) = (op first second):rest
rpnEval _ _                               = error "Stack underflow"

operator name = case name of "+" -> (+); "-" -> (-); "*" -> (*); "/" -> (/)
                             _ -> error $ "Bad operator: " ++ name

