Add line editing
This commit is contained in:
parent
86dd293001
commit
ed5d0a58d3
23
hsilop.hs
23
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
|
||||
|
Loading…
Reference in New Issue
Block a user