<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://tcl.activestate.com/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Wed May 16 19:58:31 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='201'>
<header><title>Add &apos;in&apos; Operator to [expr]</title><author address="mailto:jeffh@activestate.com">Jeff Hobbs</author><status type='project' state='final' tclversion="8.5" vote='after'>$Revision: 1.5 $</status><history></history><created day='21' month='may' year='2004' /><keyword>Tk {list membership} sets</keyword></header>
<abstract>This TIP proposes new <emph style="bold">expr</emph> operators &quot;<emph style="bold">in</emph>&quot; and &quot;<emph style="bold">ni</emph>&quot; that simplifies the standard &quot;does this item exist in list&quot; <emph style="bold">lsearch</emph> case in expressions, with &quot;<emph style="bold">ni</emph>&quot; being &quot;not in&quot;.</abstract>
<body><section title="Rationale">
<para>The addition of <emph style="bold">in</emph> and <emph style="bold">ni</emph> operators is syntactic sugar for the verbose <emph style="bold">lsearch</emph> operation that checks for a single items existence (or non-existence) in a list. It serves to simplify the reading and writing of code that requires this comparatively-common operation. The operators will do <emph style="bold">lsearch -exact</emph> searching as well, which would correct an inadvertant bug that many users introduce into their code when they write:</para>
<verbatim><vline encoding='base64'>IGlmIHtbbHNlYXJjaCAkbGlzdCAkaXRlbV0gIT0gLTF9IHsgLi4uIH0=</vline></verbatim>
<para>as <emph style="bold">lsearch</emph> does <emph style="bold">-glob</emph> matching by default. I have found this error repeated often in user code.</para>
<para>Note that <tipref type="text" tip="133"/> uses an <emph style="bold">in</emph> operator as a motivating case, but is actually proposing a much more general alteration.</para>
</section>
<section title="Specification">
<para>A new infix <emph style="bold">expr</emph> operators <emph style="bold">in</emph> and <emph style="bold">ni</emph> will be added, making the following pairs of commands equivalent (assuming nobody has redefined <emph style="bold">lsearch</emph> of course):</para>
<itemize><item.i><para>Searching for an arbitrary item in an arbitrary list.</para><verbatim><vline encoding='base64'>IGlmIHtbbHNlYXJjaCAtZXhhY3QgJGxpc3QgJGl0ZW1dICE9IC0xfSB7IC4uLiB9</vline><vline encoding='base64'>IGlmIHskaXRlbSBpbiAkbGlzdH0geyAuLi4gfQ==</vline></verbatim></item.i><item.i><para>Checking for the absence of an arbitrary item from an arbitrary list.</para><verbatim><vline encoding='base64'>IGlmIHtbbHNlYXJjaCAtZXhhY3QgJGxpc3QgJGl0ZW1dID09IC0xfSB7IC4uLiB9</vline><vline encoding='base64'>IGlmIHskaXRlbSBuaSAkbGlzdH0geyAuLi4gfQ==</vline></verbatim></item.i></itemize>
</section>
<section title="Comments">
<para>There was a proposal to add a separate operator &quot;<emph style="bold">!in</emph>&quot; to do negated membership testing, but that significantly complicates the expression parser and is likely to be harder to teach to newcomers to Tcl, since it looks like the application of an operator to another operator.</para>
<para><emph style="bold">notin</emph> and <emph style="bold">ni</emph> were also suggested, and <emph style="bold">ni</emph> was accepted as a reasonable pair for &quot;not in&quot;, similar to <emph style="bold">ne</emph> being &quot;not equal&quot;. There was some concern about <emph style="bold">ni</emph> in that TeX has a different interpretation, but that is unlikely to confuse the majority of Tcl users.</para>
</section>
<section title="Reference Implementation">
<para><emph style="italic">[To Follow]</emph></para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

