From 9f8e83010903509d338722c4a2adef295e8ea67e Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Mon, 22 Feb 2016 14:46:02 +0000 Subject: Query for formula indefinetly --- rpn.hs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/rpn.hs b/rpn.hs index dbb0f1f..68c0831 100755 --- a/rpn.hs +++ b/rpn.hs @@ -1,4 +1,6 @@ -import Control.Monad (join, sequence) +#!/usr/bin/env runghc + +import Control.Monad (join, sequence, forever) import System.Environment (getArgs) import Data.Maybe (listToMaybe, isJust) import Data.Char (isSpace) @@ -39,25 +41,25 @@ fromSymbol _ = (0, 0) pPrint :: Show a => Symbol a -> String -pPrint (Number x d) = (show x) ++ " ± " ++ (show d) +pPrint (Number x d) = (show x) ++ " ± " ++ (show d) ++ " (relative error: " ++ (show $ d / x) ++ ")" pPrint s = show s main :: IO () main = do hSetBuffering stdin LineBuffering hSetBuffering stdout NoBuffering - (mode:args) <- getArgs - let symbols = join . join $ map (map words) . map lines $ args + forever $ do + putStr "Formula: " + line <- getLine + let --symbols = join . join $ map (map words) . map lines $ args + symbols = words line unknownSymbols = nub $ filter (not . isKnownSymbol) symbols - symbolMap <- sequence $ map (sequencePair . (\x -> (x, clarifySymbol x))) unknownSymbols - let symbolMap' = M.fromList symbolMap - symbols' = map (\x -> if x `elem` (M.keys symbolMap') then symbolMap' M.! x else [x]) symbols - symbols'' = map interpreteSymbol $ join symbols' - let result = evalExpVec symbols'' - case mode of - "n" -> putStrLn . show . fst $ fromSymbol result - "e" -> putStrLn . show . snd $ fromSymbol result - _ -> putStrLn $ pPrint result + symbolMap <- sequence $ map (sequencePair . (\x -> (x, clarifySymbol x))) unknownSymbols + let symbolMap' = M.fromList symbolMap + symbols' = map (\x -> if x `elem` (M.keys symbolMap') then symbolMap' M.! x else [x]) symbols + symbols'' = map interpreteSymbol $ join symbols' + let result = evalExpVec symbols'' + putStrLn . pPrint $ evalExpVec symbols'' clarifySymbol :: String -> IO [String] clarifySymbol s = do -- cgit v1.2.3