-------------------- -- Conversion of infix to postfix (W/O checking validity) -- different core f's as we build complexity post cs = f cs "" -- simple predicates and other utilities isAdd = (`elem` "+-") isMul = (`elem` "*/") notPar = (/='(') (&) p q x = p x && q x space = foldr (\x y->x:' ':y) "" -------------------- -- Stupid version (no spaces, only addops) {- f (c:cs) s | isDigit c = c : ' ' : f cs s | isAdd c = space s ++ f cs [c] f [] s = space s -} -------------------- -- Simple version (no parens, single digit) {- f (c:cs) s | isSpace c = f cs s | isDigit c = c : ' ' : f cs s | isMul c = let (m,t) = span isMul s in space m ++ f cs (c:t) | isAdd c = space s ++ f cs [c] f [] s = space s -} -------------------- -- Complex version (precedence and parens) -- {- f ('(':cs) s = f cs ('(':s) f (')':cs) s = let (o,t) = span notPar s in space o ++ f cs (drop 1 t) f (c:cs) s | isSpace c = f cs s | isDigit c = let (n,ds) = span isDigit cs in c:n ++ ' ' : f ds s | isMul c = let (m,t) = span (isMul & notPar) s in space m ++ f cs (c:t) | isAdd c = let (o,t) = span notPar s in space o ++ f cs (c:t) f [] s = space s -- -}