0
$\begingroup$

I'm new in Mathematica so this is maybe super easy to do, but I can't figure out how.

I've got a function plus[a,b] that sums two numbers and I need to implement a function derivation[a] that will derive the input like this:

derivation[e_Integer] (* returns 0 *)
derivation[x] (* returns 1 *)

I've got a problem with this rule:

derivation[plus[a, b]] := plus[derivation[a], derivation[b]];

When I implement it this way, it doesn't work because plus is evaluated before derivation. I.E. derivation[plus[x, 3]] should return 1. Is there some other way how to implement it?

$\endgroup$
2
  • 2
    $\begingroup$ Is there a reason you're not using the built-in D or Derivative? You might want to look at giving your derivation a HoldAll attribute. $\endgroup$ – rm -rf Oct 2 '13 at 15:13
  • $\begingroup$ Awesome, that's the attribute I was looking for! Thanks a lot. Yes I know about built-in Derivative, but I need to implement it on my own for learning purposes. $\endgroup$ – Micer Oct 2 '13 at 15:40
1
$\begingroup$

Just to not leave this unanswered, as rm -rf suggested in comment, setting the attribute HoldAll to derivation function solved my problem.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.