I want to define a function f[x,y]
that is Listable
for only the first argument, not the second. If we use SetAttributes[f, Listable]
, then f
will be "Listable" for all arguments. For example, we define
f[x_, y_] := {x, y}
SetAttributes[f, Listable]
f[{1, 2, 3}, y] (* -> {{1, y}, {2, y}, {3, y}} *)
f[{1, 2, 3}, {a, b, c}] (* -> {{1, a}, {2, b}, {3, c}} *)
If f
is Listable
for only the first variable, I expect
f[{1, 2, 3}, {a, b, c}]
(* -> {{1, {a, b, c}}, {2, {a, b, c}}, {3, {a, b, c}}} *)
Namely, no matter what y
is, I want to always get
f[{1, 2, 3}, y] (* -> {{1, y}, {2, y}, {3, y}} *)
Similar to HoldAll
, HoldFirst
, HoldRest
, etc, I think it is natural to have something like ListableAll
, ListableFirst
, ListableRest
, etc. I cannot understand why it is not a built-in capability of Mathematica. Is there a way to define a function that is Listable
for some but not all variables?
Listable
satisfiesListableAll
, so you don't anything new for that. $\endgroup$