hsilop (empty) → 0.1.0.0
raw patch · 5 files changed
+122/−0 lines, 5 filesdep +basedep +haskelinesetup-changed
Dependencies added: base, haskeline
Files
- LICENSE +3/−0
- README.md +23/−0
- Setup.hs +2/−0
- hsilop.cabal +30/−0
- src/Main.hs +64/−0
+ LICENSE view
@@ -0,0 +1,3 @@+Dual licensed under the MIT and GPL licenses:+http://www.opensource.org/licenses/mit-license.php+http://www.gnu.org/licenses/gpl.html
+ README.md view
@@ -0,0 +1,23 @@+# ʜƨiloꟼ+### RPN calculator in haskell++```python+$ hsilop+ꟼ 1 2 sqrt / acos+∘ 0.7853981634++ꟼ 5 8 9 2 * cos ^ - 2 0.452 tan / ++∘ 5.1717299516++ꟼ e pi ^ pi -+∘ 19.9990999792++ꟼ 1 + 2+Ꞥ∘ syntax error+```++## License++Dual licensed under the MIT and GPL licenses: +http://www.opensource.org/licenses/mit-license.php +http://www.gnu.org/licenses/gpl.html
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hsilop.cabal view
@@ -0,0 +1,30 @@+name: hsilop+version: 0.1.0.0+synopsis: RPN calculator+description:++ ʜƨiloꟼ is a simple reverse polish notation calculator+ with haskeline line editing capabilites.++homepage: https://github.com/Rnhmjoj/hsilop+license: MIT+license-file: LICENSE+author: Rnhmjoj+maintainer: micheleguerinirocco@me.com+copyright: +category: Math+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/rnhmjoj/hsilop++executable hsilop+ main-is: Main.hs+ hs-source-dirs: src+ default-language: Haskell2010+ other-extensions: ViewPatterns+ build-depends: base ==4.7.*, haskeline ==0.7.*+ ghc-options: -O2
+ src/Main.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE ViewPatterns #-}++import Data.List+import Data.Maybe+import Text.Read+import Text.Printf+import Control.Monad+import System.Console.Haskeline++main :: IO ()+main = runInputT defaultSettings repl++repl :: InputT IO ()+repl = do+ line <- getInputLine "ꟼ "+ case fromMaybe "" line of+ "q" -> return ()+ "" -> outputStrLn "" >> repl+ exp -> outputStrLn (result (rpn exp) ++ "\n") >> repl+++-- 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"+++-- Solve a RPN expression+rpn :: String -> Either String Double+rpn = foldM parse [] . words >=> return . head where + parse (y:x:xs) (flip lookup dyad -> Just f) = Right (f x y : xs)+ parse (x:xs) (flip lookup monad -> Just f) = Right (f x : xs)+ parse xs (flip lookup nilad -> Just k) = Right (k : xs)+ parse xs (readMaybe -> Just x) = Right (x : xs)+ parse _ _ = Left "syntax error"+++-- dyadic functions+dyad = [ ("+", (+))+ , ("-", (-))+ , ("*", (*))+ , ("/", (/))+ , ("^", (**)) ]++-- monadic functions+monad = [ ("sin" , sin )+ , ("asin" , asin)+ , ("cos" , cos )+ , ("acos" , acos)+ , ("tan" , tan )+ , ("atan" , atan)+ , ("ln" , log )+ , ("sqrt" , sqrt)+ , ("sgn" , signum)+ , ("abs" , abs) + , ("floor", fromIntegral . floor)+ , ("ceil" , fromIntegral . ceiling) ]++-- niladic functions+nilad = [ ("pi" , pi)+ , ("e" , exp 1)+ , ("phi", (1 + sqrt 5)/2) ]