Last active
November 17, 2015 21:54
-
-
Save ayberkt/9b6674e8495fc48b53ba to your computer and use it in GitHub Desktop.
Tiny polish notation arithmetic interpreter in Haskell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- 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