title: How to handle expressions in Haskell?
body: <p>Let's say I have :</p>
  
  <pre><code>f :: Double -&gt; Double
  f x = 3*x^2 + 5*x + 9
  </code></pre>
  
  <p>I would like to compute the derivative of this function and write</p>
  
  <pre><code>derivate f
  </code></pre>
  
  <p>so that</p>
  
  <pre><code>derivate f == \x -&gt; 6*x + 5
  </code></pre>
  
  <p>but how to define <code>derivate</code>?</p>
  
  <pre><code>derivate :: (a -&gt; a) -&gt; (a -&gt; a)
  derivate f = f' -- how to compute f'?
  </code></pre>
  
  <p>I'm aware there is no native way to do this, but is there a library that can?</p>
  
  <p>Do we have to rely on "meta"-datatypes to achieve this?</p>
  
  <pre><code>data Computation = Add Exp Expr | Mult Expr Expr | Power Expr Expr -- etc
  </code></pre>
  
  <p>Then, is it not a pain to make a corresponding constructor for each function ? However, datatypes should not represent functions (except for parsers).</p>
  
  <p>Is <a href="https://en.wikipedia.org/wiki/Pure_%28programming_language%29" rel="nofollow noreferrer">Pure</a> a good alternative because of its term-rewriting feature? Doesn't it have its drawbacks as well?</p>
  
  <p>Are lists affordable?</p>
  
  <pre><code>f :: [Double]
  f = [3, 5, 9]
  
  derivate :: (a -&gt; [a])
  derivate f = (*) &lt;$&gt; f &lt;*&gt; (getNs f)
  
  compute f x = sum $
      ((*) . (^) x) &lt;$&gt; (getNs f) &lt;*&gt; f
  
  getNs f = (reverse (iterate (length f) [0..]))
  </code></pre>
  
  <p>Haskell now looks like it depends on LISP with a less appropriate syntax. Function and arguments waiting to be used together are quite stored in datatypes.
  Plus, it's not very natural.</p>
  
  <p>They don't seem to be "flexible" enough to be able my <code>derivate</code> function other than polynomials, such as homographic functions.</p>
  
  <p>Right now, for example, I would like to use derivatives for a game. The character runs on a floor made using a function, and I would like him to slide if the floor is steep enough.</p>
  
  <p>I also need to solve equations for various purposes. Some examples:</p>
  
  <p>I'm a spaceship and I want to take a nap. During my sleep, if I don't place myself carefully, I might crash on a planet because of gravity. I don't have enough gas to go far away from celestial objects and I don't have a map either.
  So I must place myself between the objects in this area so that the sum of their gravitationnal influence on me is canceled.
  <code>x</code> and <code>y</code> are my coordinates. <code>gravity</code> is a function that takes two objects and return the vector of the gravitationnal force between them.
  If there are two objects, say the Earth and the Moon, besides me, all I need to do to find where to go is to solve:</p>
  
  <pre><code>gravity earth spaceship + gravity moon spaceship == (0, 0)
  </code></pre>
  
  <p>It's much simpler and faster, etc., than to create a new function from scratch <code>equigravityPoint :: Object -&gt; Object -&gt; Object -&gt; Point</code>.</p>
  
  <p>If there are 3 objects besides me, it's still simple.</p>
  
  <pre><code>gravity earth spaceship + gravity moon spaceship + gravity sun spaceship == (0, 0)
  </code></pre>
  
  <p>Same for 4, and n. Handling a list of objects is much simpler this way than with <code>equigravityPoint</code>.</p>
  
  <p>Other example.
  I want to code an ennemy bot that shoots me.
  If he just shoots targeting my current position, he will get me if I run towards me, but he'll miss me if I jump and fall on him.
  A smarter bot thinks like that: "Well, he jumped from a wall. If I shoot targeting where he is now the bullet won't get him, because he will have moved until then. So I'm gonna anticipate where he'll be in a few seconds and shoot there so that the bullet and him reach this point at the same time".
  Basically, I need the ability to compute trajectories. For example, for this case, I need the solution to <code>trajectoryBullet == trajectoryCharacter</code>, which gives a point where the line and the parabola meet.</p>
  
  <p>A similar and simpler example not involving speed.
  I'm a fireman bot and there's a building in fire. Another team of firemen is fighting the fire with their water guns. I am and there are people jumping from . While my friends are shooting water, I hold the trampoline.
  I need to go where the people will fall before they do. So I need trajectories and equation-solving.</p>