Skip to content

Instantly share code, notes, and snippets.

@ayberkt
Last active November 17, 2015 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayberkt/9b6674e8495fc48b53ba to your computer and use it in GitHub Desktop.
Save ayberkt/9b6674e8495fc48b53ba to your computer and use it in GitHub Desktop.
Tiny polish notation arithmetic interpreter in Haskell.
{- Based on Miran Lipovaca's RPN solver from LYAH. -}
import Control.Monad (forever)
apply :: String -> [Double] -> [Double]
apply "*" (x:y:ys) = (x * y):ys
apply "+" (x:y:ys) = (x + y):ys
apply "-" (x:y:ys) = (x - y):ys
apply "/" (x:y:ys) = (x / y):ys
apply "exp" (x:y:ys) = (x ** y):ys
apply numberString xs = (read numberString):xs
eval :: String -> Double
eval = head . foldr apply [] . words
main :: IO ()
main = forever $ putStr "> " >> getLine >>= \e -> print (eval e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment