[Haskell-cafe] Convert a function to a string of operators?

Job Vranish job.vranish at gmail.com
Fri Mar 4 22:10:19 CET 2011


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 <evgeny.grablyk at gmail.com>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
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110304/c2abb0d0/attachment.htm>


More information about the Haskell-Cafe mailing list