Skip to content

Instantly share code, notes, and snippets.

@DuoSRX
Created May 6, 2011 09:44
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 DuoSRX/958697 to your computer and use it in GitHub Desktop.
Save DuoSRX/958697 to your computer and use it in GitHub Desktop.
solveRPN :: String -> Float
solveRPN = head . foldl ff [] . words
where ff (x:y:ys) "*" = (x * y):ys
ff (x:y:ys) "+" = (x + y):ys
ff (x:y:ys) "-" = (y - x):ys
ff (x:y:ys) "/" = (y / x):ys
ff (x:y:ys) "^" = (y ** x):ys
ff (x:y:ys) "log" = (logBase x y):ys
ff (x:xs) "ln" = log x:xs
ff xs "sum" = [sum xs]
ff xs "pi" = pi:xs
ff xs numberString = read numberString:xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment