\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#295: Enhance Arguments to lrange}
\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 \#295}&\textbf{Enhance Arguments to lrange}\\\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\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes an enhancement to \textbf{lrange} and \textbf{string range} that lets them take more than one start-end index pair.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Sometimes you need to extract a non-continuous selection of elements from some list and build a new list from them. This requires the use of multiple calls to \textbf{lrange} and a surrounding \textbf{concat} to build the final list. However, since the following fails with a syntax error:
\begingroup\small\begin{verbatim}
    lrange {a b c d e f}  1 3  5 5
\end{verbatim}
\endgroup

It would seem a reasonable extension for such a usage of \textbf{lrange} to return ``b c d f''. This would also make following pattern of usage feasible:
\begingroup\small\begin{verbatim}
 lassign [lrange $someList 0 2  10 12]  var0 var1 var2 var10 var11 var12
\end{verbatim}
\endgroup

The indices should \textit{not} be required to be in ascending order. This is meant such that the ranges may be overlapping at will. E.g.:
\begingroup\small\begin{verbatim}
 [lrange $list 0 end 0 end] == [lrepeat 2 {*}$list]
\end{verbatim}
\endgroup

This TIP does NOT change the way each range is interpreted. Reversing lists is not in the scope of this TIP.

\section{Proposed Change}

Change the implementation of \textbf{lrange} to accept any odd number of arguments, with semantics (upon supply of a correct number of arguments) equivalent to:
\begingroup\small\begin{verbatim}
 proc lrange-new {list args} {
    set result [list]
    foreach {start end} $args {
       lappend result {*}[lrange $list $start $end]
    }
    return $result
 }
\end{verbatim}
\endgroup

\section{Draft Implementation}

Just the above mentioned procedure, at the moment.

\section{Further Thoughts}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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