
Hello everybody, I checked the topics in Haskell-Cafe about prefix/infix operators but I did not find them. Now I bring my questions to here to ask you. 1) I have a Common-Lisp code representing an expression result as follows ((MPLUS SIMP) ((MTIMES SIMP) ((RAT SIMP) 1 2) ((MEXPT SIMP) $X 2)) ((MTIMES SIMP) ((RAT SIMP) 1 3) ((MEXPT SIMP) $X 3))) 2) I attempted to change it to mathematics operators, replacing MPLUS SIMP -> + MEQUAL SIMP -> = RAT SIMP -> / MEXPT SIMP -> ^ translate :: String -> String translate [] = [] translate ('(':'M':'P':'L':'U':'S':' ':'S':'I':'M':'P':')':xs) = "(+)" ++ translate xs translate ('(':'M':'T':'I':'M':'E':'S':' ' :'S':'I':'M':'P':')':xs) = "(*)" ++ translate xs translate ('(':'M':'E':'Q':'U':'A':'L':' ' :'S':'I':'M':'P':')':xs) = "(=)" ++ translate xs translate ('(':'R':'A':'T':' ':'S':'I':'M':'P':')':xs) = "(/)" ++ translate xs translate ('(':'M':'L':'I':'S':'T':' ':'S':'I':'M':'P':')':xs) = "([])" ++ translate xs translate ('(':'M':'E':'X':'P':'T':' ':'S':'I':'M':'P':')':xs) = "(^)" ++ translate xs translate ('$':'X':xs) = "x" ++ translate xs translate ('$':'Y':xs) = "y" ++ translate xs translate (x:xs) = x:translate xs 3) And NOW I want to transfer from prefix operator into infix operator, for example: From ((+) ((*) ((/) 1 2) ((^) x 2)) ((*) ((/) 1 3) ((^) x 3))) in to the expression: 1/2*x^2+1/3*x^3 I try to figure out it, but this time it is not successfully. If you are familiar with that, please share with me. Many thanks to all. S.

On Jul 7, 2006, at 8:57 AM, Sara Kenedy wrote:
Hello everybody,
I checked the topics in Haskell-Cafe about prefix/infix operators but I did not find them. Now I bring my questions to here to ask you.
1) I have a Common-Lisp code representing an expression result as follows ((MPLUS SIMP) ((MTIMES SIMP) ((RAT SIMP) 1 2) ((MEXPT SIMP) $X 2)) ((MTIMES SIMP) ((RAT SIMP) 1 3) ((MEXPT SIMP) $X 3)))
2) I attempted to change it to mathematics operators, replacing
MPLUS SIMP -> + MEQUAL SIMP -> = RAT SIMP -> / MEXPT SIMP -> ^
[snip some code]
3) And NOW I want to transfer from prefix operator into infix operator, for example: From ((+) ((*) ((/) 1 2) ((^) x 2)) ((*) ((/) 1 3) ((^) x 3)))
in to the expression: 1/2*x^2+1/3*x^3
I try to figure out it, but this time it is not successfully. If you are familiar with that, please share with me. Many thanks to all.
If I were approaching this problem, I'd probably think of it like a very small compiler. That is, I would 1) define the abstract syntax as a algebraic data type 2) parse the S-expression into the abstract syntax and 3) pretty-print the new concrete syntax with infix operators. Even for a language as small as this one, I think the benefits of approaching the problem in a modular way outweigh the overhead. I think it would be very difficult to get, eg, operator precedence correct by just doing text manipulations on the string. Of course, since the source is s-expressions anyway, there's always the option of writing a lisp macro to do the whole thing and thereby get parsing for free. As a mostly related aside, here's a cool looking tutorial google turned up about writing a scheme interpreter in Haskell: http://halogen.note.amherst.edu/~jdtang/scheme_in_48/tutorial/ overview.html Rob Dockins Speak softly and drive a Sherman tank. Laugh hard; it's a long way to the bank. -- TMBG
participants (2)
-
Robert Dockins
-
Sara Kenedy