Why does Join not throw an error message when attempting to concatenate a List with an Integer, using infix notation?

image

Suppose I have my10List, a list of the ten integers from 1 to 10. Suppose I wish to rearrange the list so that the element 8 appears at the end; in other words, I wish to obtain the list {1, 2, 3, 4, 5, 6, 7, 9, 10, 8}.

Of course, there are numerous ways to accomplish this. One way is to use Join to join three sublists obtained using Part ([[]]) and Span (;;). One can do this using Join with the standard function notation or with infix notation:

Note that the braces ({}) around my10List[[8]] -- that is, {my10List[[8]]} -- are necessary because Join "concatenates lists or other expressions that share the same head," to quote the documentation. Obviously, my10List[[8]] does NOT have head List (i.e., executing Head[my10List[[8]]] gives Integer), but {my10List[[8]]} does (i.e., executing Head[{my10List[[8]]}] gives List).

Once, however, I mistakenly forgot to put the braces around my10List[[8]]. The result for Method 1 in this case is unsurprising -- Join throws an error and then prints the call in unevaluated form:

The result for Method 2, on the other hand, is surprising to me:

Surprisingly, no error is thrown and the list simply returned without the element 8. Why is this the case?

I also tried this on version 14, and the result is the same:

Look at the full form of your infix notation version:

So you essentially have Join[_List, 3], which fits the second usage of Join:



In the non-infix version you have Join[_List, _List, n_Integer], and if the input lists don't have n levels you get a message. But when joining just one list, as in Join[_List, n_Integer] it short circuits and doesn't even check if the list has n levels.

Ask AI
#1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 #16 #17 #18 #19 #20 #21 #22 #23 #24 #25 #26 #27 #28 #29 #30 #31 #32 #33 #34 #35 #36 #37 #38 #39 #40 #41 #42 #43 #44 #45 #46 #47 #48 #49 #50 #51 #52 #53 #54 #55 #56 #57 #58 #59 #60 #61 #62 #63 #64 #65 #66 #67 #68 #69 #70