On Jul 7, 2014, at 2:46 PM, Gil Dawson < email@hidden> wrote: Is it easy to see why "{5, 3} is in {{4,2}, {5, 3}, {6, 4}}" returns false?
I dunno about "easy to see", but it can be explained.
"is in" is not testing "is an element of". It's testing "is a subsequence of".
This is easiest to see and remember if you look at strings. A string, after all, is just a sequence of characters, and "is in" works the same way for either type of sequence.
For example, "xy" is in "wxyz" is not testing whether "xy" is one of the characters in "wxyz". It's testing whether the sequence of characters ("x" followed by "y") is a subsequence of the sequence of characters ("w" followed by "x" followed by "y" followed by "z").
By analogy, {5,3} is in {...} is testing whether {...} contains a subsequence consisting of 5 followed by 3. But {{4,2}, 5,3}, {6,4}} does not. It is a sequence of three items, each of them a pair of numbers. The first pair is {4,2}, which is not 5. The second pair is {5,3}, which is not 5. The third pair is {6,4}, which is not 5. The right-hand sequence does not contain any item equal to 5, and certainly not one followed by an item equal to 3
{5,3} is in {4,2,5,3,6,4} would have returned true.
Try writing {{5,3}} is in {{4,2}, {5,3}, {6,4}}. This returns true, because on the left you have a sequence containing one item (that item being the pair of numbers {5,3}), and on the right you have a sequence of three items (each of which is a pair of numbers). That sequence of one item is a subsequence of the sequence of three items.
You would also get true for {{5,3}, {6,4}} is in {{4,2}, {5,3}, {6.4}}. Now you're testing for a sequence of two pairs to be a subsequence of a sequence of three pairs, and in this case it is.
What throws your intuition off is that when you use a non-sequence in a context that requires a sequence, for example as the left-hand operand of "is in", the non-sequence is promoted to a singleton sequence.
That is, If you write 5 is in {4,5,6}, the 5 gets promoted to {5}, and that sequence is a subsequence of {4,5,6}. You get the same result you would have expected if "is in" meant "is an element of", reinforcing your incorrect intuition.
{ } is in { ... } always returns true, because every sequence contains the empty subsequence.
-Ron Hunsinger
|