diff options
-rwxr-xr-x | rpn.hs | 16 |
1 files changed, 9 insertions, 7 deletions
@@ -1,7 +1,8 @@ | |||
1 | #!/usr/bin/env runghc | 1 | #!/usr/bin/env runghc |
2 | 2 | ||
3 | import Control.Monad (join, sequence, forever) | 3 | import Control.Monad (join, sequence, forever) |
4 | import System.Environment (getArgs) | 4 | import Control.Applicative |
5 | import System.Environment | ||
5 | import Data.Maybe (listToMaybe, isJust) | 6 | import Data.Maybe (listToMaybe, isJust) |
6 | import Data.Char (isSpace) | 7 | import Data.Char (isSpace) |
7 | import Data.List (nub) | 8 | import Data.List (nub) |
@@ -49,8 +50,7 @@ main = do | |||
49 | hSetBuffering stdin LineBuffering | 50 | hSetBuffering stdin LineBuffering |
50 | hSetBuffering stdout NoBuffering | 51 | hSetBuffering stdout NoBuffering |
51 | forever $ do | 52 | forever $ do |
52 | putStr "Formula: " | 53 | line <- maybe (putStr "Formula: " >> getLine) (<$ unsetEnv "FORMULA") =<< lookupEnv "FORMULA" |
53 | line <- getLine | ||
54 | let --symbols = join . join $ map (map words) . map lines $ args | 54 | let --symbols = join . join $ map (map words) . map lines $ args |
55 | symbols = words line | 55 | symbols = words line |
56 | unknownSymbols = nub $ filter (not . isKnownSymbol) symbols | 56 | unknownSymbols = nub $ filter (not . isKnownSymbol) symbols |
@@ -62,10 +62,12 @@ main = do | |||
62 | putStrLn . pPrint $ evalExpVec symbols'' | 62 | putStrLn . pPrint $ evalExpVec symbols'' |
63 | 63 | ||
64 | clarifySymbol :: String -> IO [String] | 64 | clarifySymbol :: String -> IO [String] |
65 | clarifySymbol s = do | 65 | clarifySymbol s = maybe clarify' (return . words) =<< lookupEnv s |
66 | putStr $ "Define " ++ s ++ ": " | 66 | where |
67 | line <- getLine | 67 | clarify' = do |
68 | return $ words line | 68 | putStr $ "Define " ++ s ++ ": " |
69 | line <- getLine | ||
70 | return $ words line | ||
69 | 71 | ||
70 | isKnownSymbol :: String -> Bool | 72 | isKnownSymbol :: String -> Bool |
71 | isKnownSymbol s = or [ | 73 | isKnownSymbol s = or [ |