I am trying to get a general function $f$ accept slot-holder arguments #1, #2, where the actual arguments are lists {a1, a2,...} and {b1, b2,....}. I thought that this would involve some combination of the functions "Map" and "Apply" - I have experimented with each function individually (I don't know how to combine them) but so far I have not managed to get the desired results.
I work only with "Apply", and this has no problem when the passed-in arguments are just single variables (not lists):
Apply[f[#1, #2] &, {0, 1}]
The outcome is: f[0, 1]
When I try to pass in lists, for example #1 should accept {0,2} and #2 should accept {1,2} to produce a list {f[0,1], f[2,2]}, the output is instead:
f[{0, 2}, {1, 2}]
The code I used is:
Apply[f[#1, #2] &, {{0, 2}, {1, 2}}]
However, I noticed that if the function is explicitly defined, for example take a simple function like f[#1, #2]=#1+#2, then the output is correct.
Apply[#1 + #2 &, {{0, 2}, {1, 2}}]
which produces:
{1, 4}
I also try to use "Map" (or /@), but it only works with a single slot-holder argument. For example
f[#] & /@ {a, b, c, d, e}
gives:
{f[a], f[b], f[c], f[d], f[e]}
but if I put:
f[#1, #2] & /@ {{a, b, c, d, e}, {m, n, p, q, r}}
the result is:
{f[{a, b, c, d, e}, #2], f[{m, n, p, q, r}, #2]}
and not the desired result, which should be:
{f[a,m], f[b,n], f[c,p], f[d,q], f[e,r]}
I would be grateful if anyone could help me with this.
f @@@ {{0, 2}, {1, 2}}
you will get{f[0,2], f[1,2]}
. Or,Map[Apply[f], {{0, 2}, {1, 2}}]
$\endgroup$MapThread
andThread
and also for theListable
attribute. That should give you a better understanding of how to manipulate lists with functions. $\endgroup$f @@@ Transpose@{{a, b, c, d, e}, {m, n, p, q, r}}
, too. $\endgroup$