Add line editing

This commit is contained in:
Rnhmjoj 2015-02-28 00:27:52 +01:00
parent 86dd293001
commit ed5d0a58d3

View File

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