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 #-}
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