codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
parse :: String -> Either Double (Double -> Double -> Double) parse s = maybe (Left $ read s) Right (lookup s ops) where ops = zip ["+","-","*","/"] [(+),(-),(*),(/)] go :: [Double] -> [String] -> [Double] go stack [] = stack go stack (w:ws) = case parse w of Left x -> go (x:stack) ws Right f -> go (apply f stack) ws where apply f (x:y:xs) = (f x y:xs) eval :: [[String]] -> [Double] eval = map head . tail . scanl go [] main = getContents >>= mapM_ print . eval . map words . lines
Private
[
?
]
Run code
Submit