I'm sure there is a simple solution to this using map or apply, but its not occurring to me.

Suppose I have a function $f(x,y,z)= x+y+z)$

And I want to evaluate $f(x,y,1)$ for $\{x,y\} ∈ \{\{1,2\},\{3,4\}\}$

What is the best way to do this?


If I use f[x_,y_,z_]:= x+y+z; f[#1,#2,1]&/@ {{1,2},{3,4}}

this will give me things like {{2+#2,3+#2}}.

On the other hand, Apply works on a single element f[#1,#2,1]& @@ {1,2} but not on a list such as {{1,2},{3,4}}. On the list it gives me {f[1,3,1],f[2,4,1]

I am not sure how to go from the case of a single pair to a list of pairs

  • 1
    $\begingroup$ Tp apply to all cross pairs: Outer[f[#1, #2, 1] &, xList, yList] $\endgroup$
    – Bob Hanlon
    Oct 22, 2020 at 22:43

1 Answer 1

f[x_, y_, z_] := x + y + z;

Try @@@ (Apply at Level 1):

f[#,#2, 1] & @@@ {{1, 2}, {3, 4}}
{4, 8}
  • 1
    $\begingroup$ Ahhh, I think I see now. When I use @@, (which I think applies at level 0), uses List[1,2] for the first argument, and List[3,4] for the second. Whereas apply at level 1 applies the function to each part on level 1 of the expression. Thank you very much! I greatly appreciate it. $\endgroup$
    – user106860
    Oct 22, 2020 at 20:42
  • $\begingroup$ Sorry, quick follow-up question: Why use SlotSequence ## instead of f[#1,#2,1]? Both work for me in the simply example. Is it just for generality? $\endgroup$
    – user106860
    Oct 22, 2020 at 20:48
  • 1
    $\begingroup$ @user106860, I changed it to #, #2. The two ways give different results when fed a list like {{1},{1,2,3}. $\endgroup$
    – kglr
    Oct 22, 2020 at 20:54
  • $\begingroup$ Awesome. Thanks again! $\endgroup$
    – user106860
    Oct 22, 2020 at 20:55

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.