diff --git a/hsilop.hs b/hsilop.hs index ada60b5..27bc8d7 100644 --- a/hsilop.hs +++ b/hsilop.hs @@ -1,25 +1,30 @@ {-# LANGUAGE ViewPatterns #-} import Data.List +import Data.Maybe import Text.Read import Text.Printf import Control.Monad +import System.Console.Readline main :: IO () -main = io (result . rpn) - - --- Interact line-by-line -io :: (String -> String) -> IO () -io f = interact (unlines . map f . filter (not . null) . lines) +main = do + line <- readline "ꟼ " + case fromMaybe "" line of + "" -> main + "q" -> return () + exp -> do + putStrLn $ result (rpn exp) ++ "\n" + addHistory exp + main -- Pretty print RPN result/errors result :: Either String Double -> String result (Left err) = "Ꞥ∘ " ++ err -result (Right x) = printf ("ꟼ∘ " ++ format) x where - format | ceiling x == floor x = "%.0f" - | otherwise = "%.10f" +result (Right x) = printf format x where + format | ceiling x == floor x = "∘ %.0f" + | otherwise = "∘ %.10f" -- Solve a RPN expression