[ create a new paste ] login | about

Link: http://codepad.org/x3Ai1SN2    [ raw code | fork ]

polux - Haskell, pasted on Jul 31:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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


Output:
No errors or program output.


Create a new paste based on this one


Comments: