diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-03-01 05:15:01 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-03-01 05:15:01 +0100 |
commit | 1554095368e65bf6f1c4d0f0106ae4516117964a (patch) | |
tree | 1ed3af434943d7eec32c8557e2a2623c6d98627e | |
parent | 680e02eb3a6ae88adced9103dee2171b492eaaa4 (diff) | |
download | gausshs-1554095368e65bf6f1c4d0f0106ae4516117964a.tar gausshs-1554095368e65bf6f1c4d0f0106ae4516117964a.tar.gz gausshs-1554095368e65bf6f1c4d0f0106ae4516117964a.tar.bz2 gausshs-1554095368e65bf6f1c4d0f0106ae4516117964a.tar.xz gausshs-1554095368e65bf6f1c4d0f0106ae4516117964a.zip |
Support for reading formula from environment
-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 [ |