\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#296: Enhanced Syntax for Pair-Wise Indices}
\date{November 06, 2006}
\author{Andreas Leitgeb}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#296}&\textbf{Enhanced Syntax for Pair-Wise Indices}\\\hline
Author&

Andreas Leitgeb \tipmail{avl}{logic.at}
\\
Created&Monday, \(\text{6}^{\text{th}}\) November 2006\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.7 $\\
Keywords&Tcl, lrange, lreplace\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes adding another anchor \textbf{s} (for usage exclusively in end-indices) that refers to the respective start-index.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Currently, most commands that accept an index also accept the local keyword \textbf{end} as well as simple arithmetics involving this keyword.

Commands that accept a pair of indices for a range, require an explicit start and (inclusive)end index for the requested subrange.

In other languages (C++/stl, java, ...) it appears to be more common to specify ranges by start and length. Since the length has a higher tendency of being constant, this seems actually the better idea, anyway. For compatibility reasons, we just cannot simply change all the commands to take offset/length pairs rather than start/end.

However we could add a new anchor \textbf{s} to be used in the end-index, and allow simple arithmetic on it just like on the other index forms.

Specifying a subrange of length 1, one would then simply specify \textbf{s} as end-index. For a pair, ``s+1'' as end index does it.

If the start position of a subrange is obtained as result of a command, currently one typically does in tcl:
\begingroup\small\begin{verbatim}
 set offs [...]
 ... [lrange $list $offs [expr {$offs+1}]] ...
\end{verbatim}
\endgroup

A previous TIP has added support to do the following instead:
\begingroup\small\begin{verbatim}
 set offs [...]
 ... [lrange $list $offs $offs+1] ...
\end{verbatim}
\endgroup

This TIP aims at making this the new idiom:
\begingroup\small\begin{verbatim}
 ... [lrange $list [...] s+1]
\end{verbatim}
\endgroup

\section{Proposed Change}

Currently there is a function TclGetIntForIndex, which deals with the current \textbf{end} anchor. It could be accompanied with a second function (e.g., TclGetIntForIndex2) that would accept another argument for the previously calculated index (from the range-start argument), and use that in case the new anchor \textbf{s} occurs in the range-end index.

No change at all is proposed for each range's start index.

\section{Draft Implementation}

None, at the moment.

For a sketch see previous section. In bytecode, it could be done such, that if an \textbf{s}-prefix is seen for an (eventually bytecoded range-related command), then the current stack-top (the corresponding start-index) is dup'ed and then incremented or decremented according to any offset (s+\#, s-\#).

\section{Further Thoughts}

For \textbf{lreplace}, \textbf{s-1} as an end-index would actually make it an \textbf{linsert} operation.

This TIP covers all tcl commands that accept a pair of indices to specify a range, including (but not limited to) lrange, lreplace, string range, plus any third-party command written such as to use the proposed \textbf{TclGetIntForIndex2} function for parsing end-indices.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
