data Stack a = Nil | Top a (Stack a)
-- returns a stack without its top element
pop :: Stack a -> Stack a
pop Nil = Nil
pop (Top x xs) = xs
-- adds an element at the top of the stack
push :: a -> Stack a -> Stack a
push x xs = Top x xs
-- returns the element at the top of the stack
top :: Stack a -> a
top (Top x xs) = x
operator = "+-*/"
ziffer = "0123456789"
-- converts an infix to a postfix expression using a stack
infixToPostfix :: String -> Stack Char -> String
infixToPostfix "" y = ""
infixToPostfix (x:xs) y | elem x operator = infixToPostfix xs (push x y)
| elem x ziffer = x : (infixToPostfix xs y)
| (x == ')') = (top y) : (infixToPostfix xs (pop y))
| otherwise = infixToPostfix xs y
-- cleans an infix expression from undefined chars
cleanInfix :: String -> String
cleanInfix [] = []
cleanInfix (x:xs) | (elem x operator) || (elem x ziffer) || (x == '(') || (x == ')') = x:(cleanInfix xs)
| otherwise = cleanInfix xs
-- cleans an infix expresion and then converts it to postfix
convert :: String -> String
convert x = infixToPostfix (cleanInfix x) Nil