I am wondering how I can write a function to be used in the Apply
function in Mathematica? For example, I want to trivially re-implement the Or
function, I found the following
Apply[(#1 || #2)&,{a,b,c}]
is not okay since it only Or
'ed the first two elements in the list. Many thanks!
Equivalent
Equivalent
Apply works like this:
This will work, no matter how many vars, and is a general pattern:
for example
However, in the case of
Or
, this is not good enough, sinceOr
isHoldAll
and short-circuiting - that is, it stops upon firstTrue
statement, and keeps the rest unevaluated. Example:This will be ok though:
for example,
and can be used in such cases (when you don't want your arguments to evaluate). Note that this uses an undocumented form of
Function
. The mention of this form can be found in the book of R.Maeder, "Programming in Mathematica".HTH
Are you sure you are expecting the right thing from Apply? If you look in the documentation, http://reference.wolfram.com/mathematica/ref/Apply.html, you will see that Apply[f,expr] simply replaces the head of f by expr. It does not, in general, give f[expr].
If you wish to operate with function f onto expr, try f@expr or f[expr].
Perhaps you understand the above and your question really is, "how do I define some f that, when I do
Apply[f,{a,b,c}]
, does the same job asOr[a,b,c]
. Is that it?