# Inconsistent behavior of thread on empty llists

Consider the following:

Quit[]
(* {g[1, 3], g[2, 3]} *)

(* {} *)


This works similarly if instead $g$ takes only one parameter. Now look at the following:

Quit[]
f1[x_] := x+3;
f2[x_,y_] := x + y + {1,0};

(* {4, 5} *)
(* {} *)

(* {7, 7} *)
(* Thread::tdlen: Objects of unequal length in {}+5+{1,0} cannot be combined. *)
(* Thread::tdlen: Objects of unequal length in 5+{}+{1,0} cannot be combined. *)
(* 5 + {} + {1, 0} *)


All of these make sense except for the last one. If threading works on $f1$ with an empty list, why shouldn't it work on $f2$ with an empty list?

(BTW, there was no tag for threading.)

• Related (possibly duplicate): (4390), (135909) – Mr.Wizard Feb 7 '17 at 21:52
• one fix is to define f2 so that it can not accept a list as an argument , eg f2[x_?AtomQ, y_] := x + y + {1, 0} – george2079 Feb 7 '17 at 21:59

The message you see does not come from Thread but instead from the failed application of the Listable Attribute of Plus.

f2[{}, 5];


Thread::tdlen: Objects of unequal length in {}+5+{1,0} cannot be combined. >>

Listable does not work over empty lists in the way that Thread does, despite the message name being Thread::tdlen:

Attributes[foo] = Listable;

foo[{}, {1, 2, 3}]


Thread::tdlen: Objects of unequal length in foo[{},{1,2,3}] cannot be combined. >>

foo[{}, {1, 2, 3}]


If you want Thread to work in the example you give you'll need to control evaluation, e.g.

Thread[Unevaluated @ f2[{}, 5]]

{}

• OK, but if $g$ is undefined, Thread doesn't even invoke $g$ when handed an empty list (or so it looks to me). – rogerl Feb 7 '17 at 21:47
• @rogerl It's an evaluation thing; I added one more example that I hope is enlightening? Thread[g[{}, 3]] works precisely because g is undefined, i.e. it does not evaluate before Thread sees it. – Mr.Wizard Feb 7 '17 at 21:48
• Well, I see that using Unevaluated works, but I really don't see why. I kind of assumed that the argument to Thread was unevaluated anyway, since Thread needs to pick the expression apart. – rogerl Feb 7 '17 at 21:51
• @rogerl Please see the Related links (or now the Linked sidebar) I just added above. I believe your questions will be answered therein. If not feel free to ask more. :-) – Mr.Wizard Feb 7 '17 at 21:53
• OK, thanks. Feel free to close as duplicate if you think it is. Sorry I didn't find them myself. – rogerl Feb 7 '17 at 21:54