Simple question. How can we construct functions like Mathematica does?
For example, let's say that we want to create our own D
function.
The D
function works like:
In[1]:= D[x^2, x]
Out[1]= 2 x
where we can put a function in x
and the function will treat this internally in a correct way.
How can we create our D1
function with the same behavior?
Attempts:
In[1]:= D1[f_, x_] := Limit[(-f[x] + f[x + a])/a, a -> 0]
In[2]:= D1[x^2, x]
Out[2]= Limit[(-(x^2)[x] + (x^2)[a + x])/a, a -> 0]
In[3]:= D1[Function[x, x^2], x]
Out[3]= 2 x
In[4]:= D1[#^2 &, x]
Out[4]= 2 x
As we can see, the second and third example work, but I'd like to create a function that would work like in the first example and like the D
function.
How can we do this?