Skip to content

Instantly share code, notes, and snippets.

@joaomilho
Forked from softa/operation.hs
Last active August 29, 2015 14:01
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 joaomilho/54837d7b9f78504ea513 to your computer and use it in GitHub Desktop.
Save joaomilho/54837d7b9f78504ea513 to your computer and use it in GitHub Desktop.
module Main where
import Text.ParserCombinators.Parsec
runop :: Char -> Integer -> Integer -> Integer
runop '+' firstOperand secondOperand = firstOperand + secondOperand
runop '-' firstOperand secondOperand = firstOperand - secondOperand
runop '*' firstOperand secondOperand = firstOperand * secondOperand
runop '^' firstOperand secondOperand = firstOperand ^ secondOperand
operation :: Parser Integer
operation = do
firstOperand <- number
operationChar <- operator
secondOperand <- number
return (runop operationChar firstOperand secondOperand)
operator :: Parser Char
operator = do
operatorToken <- oneOf "+-*^"
return (operatorToken)
number :: Parser Integer
number = do
numberToken <- many (oneOf "0123456789")
return (read numberToken)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment