I ran into unexpected behavior in my code based on an errant assumption. Namely, I thought that:

ClearAll[aa, bb, cc, dd, ee, ff];
Thread[Equal[{aa, bb, cc}, {dd, ee, ff}]]
Thread[SameQ[{aa, bb, cc}, {dd, ee, ff}]]

would result in:

{aa == dd, bb == ee, cc == ff}
{False, False, False}

What I get instead is:

{aa == dd, bb == ee, cc == ff}

Which I can argue makes sense, since the lists aren't equivalent. But why doesn't Thread work? What's the precedence argument here? How can I get the answer I want ({False, False, False}) from a similar construct?

  • 3
    $\begingroup$ To get {False,False,False} you can use MapThread[SameQ, {{aa, bb, cc}, {dd, ee, ff}}]. $\endgroup$ – kglr Apr 18 '12 at 2:19

Thread doesn't hold its arguments. You can check its attributes.

So, before doing any threading, it evaluates its arguments.

Now, understanding the behaviour you describe requires understanding the difference between Equal and SameQ. Equal is meant for math reasoning. For expressing an equality, which might involve a variable that at the time you don't yet know it's value. So, for example, x==8 returns unevaluated if x isn't defined.

SameQ however is a predicate. It will always return either True or False if the constructs are exactly the same (after evaluation).

So, Thread[SameQ[{aa, bb, cc}, {dd, ee, ff}]] -> Thread[False]-> False

One can see this by running (thanks @rcollyer)

Trace[Thread[SameQ[{aa, bb, cc}, {dd, ee, ff}]], 
 TraceInternal -> True]

Out[1] = {{{aa, bb, cc} === {dd, ee, ff}, False}, Thread[False], False}

If you want to thread SameQ without evaluation, just use Unevaluated

Thread[Unevaluated@SameQ[{aa, bb, cc}, {dd, ee, ff}]]

{False, False, False}

Another construction that gives the result you want is the one suggested by @kguler in his comment and supported by @rcoller and his upvoters: MapThread. I'd suggest you search the docs if you don't know it

MapThread[SameQ, {{aa, bb, cc}, {dd, ee, ff}}]
  • 2
    $\begingroup$ Or, MapThread, fewer characters required. :) $\endgroup$ – rcollyer Apr 18 '12 at 2:23
  • $\begingroup$ Yeah... I personally prefer the looks of the prefix notation Thread@Unevaluated@SameQ[{aa, bb, cc}, {dd, ee, ff}] $\endgroup$ – Rojo Apr 18 '12 at 2:26
  • $\begingroup$ Whatever works. $\endgroup$ – rcollyer Apr 18 '12 at 2:26
  • $\begingroup$ I meant to write Thread@Unevaluated@({aa, bb, cc}==={dd, ee, ff}) $\endgroup$ – Rojo Apr 18 '12 at 8:05
  • $\begingroup$ Oops, I just assumed Thread held things. Sure enough though, Attributes[Thread]=={Protected}... Thanks for the suggestions on how to get around that! $\endgroup$ – tkott Apr 18 '12 at 10:24

I use Part for this sort of thing.

data = Transpose[{{aa, bb, cc}, {dd, ee, ff}}]
{{aa, dd}, {bb, ee}, {cc, ff}}

Now change the heads:

data[[All, 0]] = Equal;
{aa == dd, bb == ee, cc == ff}


data[[All, 0]] = SameQ;
{False, False, False}
  • $\begingroup$ I like the use of the 0th part to replace the head :) $\endgroup$ – tkott Apr 18 '12 at 10:24
  • $\begingroup$ I only started using it a couple of months ago but now use it a fair bit due to it being nice and concise. $\endgroup$ – Mike Honeychurch Apr 18 '12 at 11:48
  • $\begingroup$ An interesting replacement for Equal @@@ data, although in this case, I think Apply wins in character count. But, definitely something to keep in mind when working with deeper nesting. $\endgroup$ – rcollyer Apr 18 '12 at 13:17

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.