title: Haskell calculator - order of operations
body: <p>I am very new to Haskell and I need to make a working calculator what will give answers to expressions like: 2+3*(5+12)
  I have something that manages to calculate more or less but I have a problem with order of operations. I have no idea how to do it. Here is my code:</p>
  
  <pre><code>import Text.Regex.Posix
  import Data.Maybe
  
  oblicz :: String -&gt; Double
  oblicz str = eval (Nothing, None) $ map convertToExpression $ ( tokenize str )
  
  
  eval :: (Maybe Double,Expression)-&gt;[Expression]-&gt;Double
  
  eval (Nothing, _) ((Variable v):reszta) = eval (Just v, None) reszta
  eval (Just aktualnyWynik, None) ((Operator o):reszta) = eval ((Just aktualnyWynik), (Operator o)) reszta
  
  
  
  eval (Just aktualnyWynik, (Operator o)) ((Variable v):reszta) = eval (Just $ o aktualnyWynik v , None) reszta
  
  
  eval (aktualnyWynik, operator) (LeftParenthesis:reszta) 
      = eval (aktualnyWynik, operator) ((Variable (eval (Nothing, None) reszta)):(getPartAfterParentheses reszta))
  
  
  eval (Just aktualnyWynik, _) [] = aktualnyWynik
  eval (Just aktualnyWynik, _) (RightParenthesis:_) = aktualnyWynik
  
  data Expression =     Operator (Double-&gt;Double-&gt;Double)
                      | Variable Double
                      | LeftParenthesis
                      | RightParenthesis
                      | None
  
  tokenize :: String -&gt; [String]
  tokenize expression = getAllTextMatches(expression =~ "([0-9]+|\\(|\\)|\\+|-|%|/|\\*)" :: AllTextMatches [] String)
  
  convertToExpression :: String -&gt; Expression                 
  convertToExpression "-" = Operator (-)
  convertToExpression "+" = Operator (+)
  convertToExpression "*" = Operator (*)
  convertToExpression "/" = Operator (/)
  convertToExpression "(" = LeftParenthesis
  convertToExpression ")" = RightParenthesis
  convertToExpression variable = Variable (read variable)
  
  getPartAfterParentheses :: [Expression] -&gt; [Expression]
  getPartAfterParentheses [] = []
  getPartAfterParentheses (RightParenthesis:expressionsList) = expressionsList
  getPartAfterParentheses (LeftParenthesis:expressionsList) = getPartAfterParentheses (getPartAfterParentheses expressionsList)
  getPartAfterParentheses (expression:expressionsList) = getPartAfterParentheses expressionsList
  </code></pre>
  
  <p>I thought maybe I could create two stacks - one with numbers and one with operators. While reading the expression, I could push numbers on one stack and operators on another. When it is an operator I would check if there is something already on the stack and if there is check if I should pop it from the stack and do the math or not - just like in onp notation.</p>
  
  <p>Unfortunately, as I said, I am VERY new to haskell and have no clue how to go about writing this. </p>
  
  <p>Any hints or help would be nice :)</p>