# Replacing $x,y$ in $f(x,y,z)$ with values from a list

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?

Examples:

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

• Tp apply to all cross pairs: Outer[f[#1, #2, 1] &, xList, yList] Commented Oct 22, 2020 at 22:43

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


Try @@@ (Apply at Level 1):

f[#,#2, 1] & @@@ {{1, 2}, {3, 4}}

{4, 8}

• 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. Commented Oct 22, 2020 at 20:42
• 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? Commented Oct 22, 2020 at 20:48
• @user106860, I changed it to #, #2. The two ways give different results when fed a list like {{1},{1,2,3}.
– kglr
Commented Oct 22, 2020 at 20:54
• Awesome. Thanks again! Commented Oct 22, 2020 at 20:55

We can set 1 as Optional default value of z

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


Now we can do

f @@@ {{1, 2}, {3, 4}}


{4, 8}

f @@@ {{1, 2, 2}, {3, 4, 2}}


{5, 9}

f[##, 2] & @@@ {{1, 2}, {3, 4}}


{5, 9}

Since V 13.1 we can replace @@@ with MapApply (which is often useful regarding operator precedence)

MapApply[f] @ {{1, 2}, {3, 4}}


{4, 8}

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


Using Map at level 2:

m = {{1, 2}, {3, 4}};

Map[f[Sequence @@ ##, 1] &, {m}, {2}][[1]]

(*{4, 8}*)


Or using Outer:

Diagonal@Outer[f[##, 1] &, Sequence @@ Thread@m]

(*{4, 8}*)