2
$\begingroup$

Possible Duplicate:
Functions vs. patterns

I thought about describing the context in which I faced this problem, but I figured it is general enough and clear enough to pose it as it is.

What is the difference between the two following definitions:

f[x_]:=Sin[x]
g=Sin[#]&

Under what circumstances will the two behave differently? Is any of them preferable as a general practice?

$\endgroup$

marked as duplicate by Leonid Shifrin, Yves Klett, Mr.Wizard Dec 4 '12 at 11:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @LeonidShifrin well, there are the much better answers! $\endgroup$ – Yves Klett Dec 4 '12 at 11:30
  • $\begingroup$ @YvesKlett At the time that one was asked, we had a lot of energy since we were only out there for a week :) $\endgroup$ – Leonid Shifrin Dec 4 '12 at 11:31
  • $\begingroup$ @LeonidShifrin question on best practice: since my answer is now quite redundant and I am voting to close, should I delete the answer? $\endgroup$ – Yves Klett Dec 4 '12 at 11:36
  • $\begingroup$ @YvesKlett Since the question is likely going to be closed,I think that does not matter much. $\endgroup$ – Leonid Shifrin Dec 4 '12 at 11:37
  • 1
    $\begingroup$ @LeonidShifrin True that. Googling with site:mathematica.stackexchange.com added to the search terms is often better. $\endgroup$ – Sjoerd C. de Vries Dec 4 '12 at 12:52
0
$\begingroup$

Very short answer on one relevant difference (probably much better ones will appear):

A pure function like g will be faster performance-wise, because it gets rid of all the overhead (e.g. pattern matching) involved in the definition of f.

$\endgroup$

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