From 1554095368e65bf6f1c4d0f0106ae4516117964a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 1 Mar 2016 05:15:01 +0100 Subject: Support for reading formula from environment --- rpn.hs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/rpn.hs b/rpn.hs index 64fdb89..b441db4 100755 --- a/rpn.hs +++ b/rpn.hs @@ -1,7 +1,8 @@ #!/usr/bin/env runghc import Control.Monad (join, sequence, forever) -import System.Environment (getArgs) +import Control.Applicative +import System.Environment import Data.Maybe (listToMaybe, isJust) import Data.Char (isSpace) import Data.List (nub) @@ -49,8 +50,7 @@ main = do hSetBuffering stdin LineBuffering hSetBuffering stdout NoBuffering forever $ do - putStr "Formula: " - line <- getLine + line <- maybe (putStr "Formula: " >> getLine) (<$ unsetEnv "FORMULA") =<< lookupEnv "FORMULA" let --symbols = join . join $ map (map words) . map lines $ args symbols = words line unknownSymbols = nub $ filter (not . isKnownSymbol) symbols @@ -62,10 +62,12 @@ main = do putStrLn . pPrint $ evalExpVec symbols'' clarifySymbol :: String -> IO [String] -clarifySymbol s = do - putStr $ "Define " ++ s ++ ": " - line <- getLine - return $ words line +clarifySymbol s = maybe clarify' (return . words) =<< lookupEnv s + where + clarify' = do + putStr $ "Define " ++ s ++ ": " + line <- getLine + return $ words line isKnownSymbol :: String -> Bool isKnownSymbol s = or [ -- cgit v1.2.3