From ed5d0a58d34e3e35b82d6fbe9c66ab00e364932e Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Sat, 28 Feb 2015 00:27:52 +0100 Subject: [PATCH] Add line editing --- hsilop.hs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) 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