hsilop/RPN.hs

48 lines
1.2 KiB
Haskell
Raw Normal View History

2015-02-26 19:50:12 +01:00
{-# LANGUAGE ViewPatterns #-}
import Data.List
2015-02-26 23:12:56 +01:00
import Text.Read
import Control.Monad
2015-02-26 19:50:12 +01:00
main :: IO ()
main = io (show . rpn)
io :: (String -> String) -> IO ()
io f = interact (unlines . map f . lines)
2015-02-26 23:12:56 +01:00
rpn :: String -> Either String Double
rpn = return . head <=< foldM parse [] . words
parse :: [Double] -> String -> Either String [Double]
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 x = case readMaybe x of
Just x -> Right (x : xs)
Nothing -> Left "Syntax error"
2015-02-26 19:50:12 +01:00
-- 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) ]