Initial commit
This commit is contained in:
commit
da8897b238
42
RPN.hs
Normal file
42
RPN.hs
Normal file
@ -0,0 +1,42 @@
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
|
||||
import Data.List
|
||||
|
||||
main :: IO ()
|
||||
main = io (show . rpn)
|
||||
|
||||
io :: (String -> String) -> IO ()
|
||||
io f = interact (unlines . map f . lines)
|
||||
|
||||
rpn :: String -> Double
|
||||
rpn = head . foldl parse [] . words where
|
||||
parse (y:x:xs) (flip lookup dyad -> Just f) = f x y : xs
|
||||
parse (x:xs) (flip lookup monad -> Just f) = f x : xs
|
||||
parse xs (flip lookup nilad -> Just k) = k : xs
|
||||
parse xs x = read x : xs
|
||||
|
||||
-- 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) ]
|
Loading…
Reference in New Issue
Block a user