[Haskell-cafe] Partial Derivatives

Twan van Laarhoven twanvl at gmail.com
Mon May 8 10:28:54 EDT 2006

Gerhard Navratil wrote:
 > I need a library that provides partial derivatives for functions. The
 > solution I came up with is based on a datatype using reversed polish
 > notation to store the function:
 > <<lots of code>>
 > The solution works but is not very elegant. The complete module is
 > appended to the mail.
 > Does anyone have a more elegant solution or is there a package that
 > provides derivatives in a similar way?

A simple way to make it more ellegant is to use smart constructors, so 
you don't have to check to prevent zeroes everywhere:

 > sub :: Fkt a -> Fkt a -> Fkt a
 > sub a b
 >   | b == 0    = a
 >   | a == 0    = Neg b
 >   | otherwise = Sub a b
 > -- etc.

Now you can use this smart constructor instead of the real constructor 
without worry, like:

 > derivative name (Sub a b) = sub (derivative name a) (derivative name b)
 > -- etc.


More information about the Haskell-Cafe mailing list