Make your own expression type and make it an instance of the Num typeclass.
Then you can build your expression using the usual operators and then use
show to convert to a string.

For example:
https://github.com/jvranish/grhug/blob/master/SymbolicDifferentiation/SymbolicDifferentiation.hs

It probably does more than you want, but you should be able to get the basic
idea.

The really slick thing about it is that you can use expression type on any
function that takes a Num and you'll get a representation of the computation
that took place to get the result.

For example:
  show (baw a b c :: Int)  -- will show you an int
and
  show (baw a b c :: Expr)  -- will give you "(a + b) * c"  (well... a, b, c
will be replace by whatever you passed in, but you can make them variable
names just the same)

- Job


On Fri, Mar 4, 2011 at 3:32 PM, Evgeny Grablyk <[email protected]>wrote:

> Hello!
>
> I was wondering if it was possible to "convert" a function (which may
> also call functions) to a plain list of operators on parameters.
> Example:
>
> foo a b = a + b
> bar a b = a * b
>
> baw a b c = bar (foo a b) c
> baw' a b c = (a + b) * c
>
> Any way to get `baw'' from `baw'?  Preferrably as a String.
>
> --
> Evgeny
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to