[Haskell-cafe] Prefix/Infix operators

Robert Dockins robdockins at fastmail.fm
Fri Jul 7 10:11:49 EDT 2006

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
>           ((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
> RAT 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:


Rob Dockins

Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
           -- TMBG

More information about the Haskell-Cafe mailing list