[Haskell-cafe] Are there arithmetic composition of functions?

Tillmann Rendel rendel at informatik.uni-marburg.de
Tue Mar 20 11:29:20 CET 2012


sdiyazg at sjtu.edu.cn wrote:
> I feel it would be very natural to have in haskell something like
>     g::Float->Float
>     --define g here
>     h::Float->Float
>     --define h here
>     f::Float->Float
>     f = g+h --instead of f t = g t+h t
>     --Of course, f = g+h is defined as f t = g t+h t

One approach to achieve this is to define your own version of +, using 
the equation in the last comment of the above code snippet:

   (g .+. h) t = g t + h t

Now you can write the following:

   f = g .+. h

You could now implement .*., ./. and so on. (I use the dots in the 
operator names because the operator is applied pointwise). The 
implementations would look like this:

   (g .+. h) t = g t + h t
   (g .*. h) t = g t * h t
   (g ./. h) t = g t / h t

This is a bit more low-tech than the proposals in the other answers, but 
might be good enough for some applications.


More information about the Haskell-Cafe mailing list