Re: A Syntax Question
Re: A Syntax Question
- Subject: Re: A Syntax Question
- From: Ron Hunsinger <email@hidden>
- Date: Mon, 07 Jul 2014 18:57:39 -0700
On Jul 7, 2014, at 4:51 PM, Ted Wrigley <email@hidden> wrote:
> It seems to be a coercion issue: apparently applescript is trying to coerce to lists before it compares them, which is throwing something off internally. for example, the following all come out as true (even though they maybe shouldn’t):
It is a coercion issue, but nothing is being thrown off.
If the right hand argument is a string, the left hand argument is coerced to a string, and a substring test is performed.
Otherwise, both arguments are coerced to lists, and a subsequence test is performed.
These expressions with a string on the right all evaluate to true (because the left hand sides all coerce to "xy"):
"xy" is in "wxyz"
{"x", "y"} is in "wxyz"
{"xy"} is in "wxyz"
and these are also true, after the left hand sides coerce to the string "45"
45 is in "3456"
5 * 9 is in "3456"
{ 2 * 2, 2 + 3} is in "3456"
{ { {4} }, { { { 5 } } } } is in "3456"
One oddity concerns the empty string vs the empty list. The empty list is a subsequence of every list, but surprisingly (and incorrectly IMHO) the empty string is never a substring of anything. These expressions are all false:
"" is in "xyz"
"" is in ""
{ } is in "xyz"
These expressions with a list on the right all evaluate to true, because the left hand argument is a subsequence of the right hand argument:
{ 5, 3 } is in { 4, 2, 5, 3, 6, 4 }
{ 5 } is in { 4, 5, 6 }
{ 5 } is in { 5 }
{ } is in { 5 }
{ } is in { }
{ {5,3} } is in { {4,2}, {5,3}, {6,4} }
{ {5,3}, {6,4} } is in { {4,2}, {5,3}, {6,4}, {7,8} }
{ "corn" } is in { "apple", "corn", "eggs" }
Coercing a non-list A to a list produces the singleton list {A}. Thus, all of the following are true:
5 is in {5}
{5} is in 5
5 is in 5
{ } is in 5
"corn" is in { "apple", "corn", "eggs" }
But coercing something that is already a list to a list returns the original list. Thus, the following is false:
{5,3} is in { {4,2}, {5,3}, {6,4} }
because {5,3} does not promote to { {5,3} }. If you want a list promoted to a list of a list, you need to do it yourself.
The important concepts to remember are:
"is in" tests for subsequence (with substring being a special case). It does not test for membership.
When a non-list on the left promotes to a list, it makes a subsequence test appear to be a membership test.
Which is probably why you thought "is in" meant "is a member of".
That, and years of Set Theory, where "x ∈ A" is pronounced "x is in A".
-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