• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: A Syntax Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Syntax Question


  • Subject: Re: A Syntax Question
  • From: Ron Hunsinger <email@hidden>
  • Date: Mon, 07 Jul 2014 16:26:44 -0700

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


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >A Syntax Question (From: Gil Dawson <email@hidden>)

  • Prev by Date: Re: last modified file - how to catch?
  • Next by Date: Re: last modified file - how to catch?
  • Previous by thread: A Syntax Question
  • Next by thread: Re: A Syntax Question
  • Index(es):
    • Date
    • Thread