\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#367: A Command to Remove Elements from a List}
\date{May 18, 2010}
\author{Donal K. Fellows}
\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 \#367}&\textbf{A Command to Remove Elements from a List}\\\hline
Author&

Donal K. Fellows \tipmail{dkf}{users.sf.net}
\\
Created&Tuesday, \(\text{18}^{\text{th}}\) May 2010\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.1 $\\
Keywords&Tcl, delete, item\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a command, \textbf{lremove}, that takes a list value and a collection of indices, and returns a list that is the input list with the elements at those indices removed.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Tcl has many operations for working with lists, such as \textbf{list} for building them, \textbf{lappend} for adding to them, \textbf{linsert} for insertion, \textbf{lreplace} for replacement of ranges, and \textbf{lset} for replacement of individual elements, but it has none that is designed to remove elements of a list. While the functionality can be simulated in the simple case with \textbf{lreplace}, this is rather more difficult when multiple indices are present. It is particularly challenging when using a mixture of indices that are defined relative to the start and the end of the list. Since the tools for doing the mapping of indices to list positions are easily available at the C level, I propose to add a command to Tcl to do the removal operation that takes advantage of the capabilities to do this all correctly.

\section{Proposed Change}

This TIP proposes adding a command, \textbf{lremove}, with the following syntax:

\begin{quote}
\textbf{lremove} \textit{list} ?\textit{index}? ?\textit{index...}?
\end{quote}

That is, the command takes one mandatory argument, \textit{list}, and an arbitrary number of \textit{index} arguments (including zero). The \textit{list} argument must be a valid Tcl list, and each of the \textit{index} arguments must be a valid Tcl index (see \cite{tip176} for a description) where \textbf{end} will refer to the last element of \textit{list}. Assuming syntactic validity, the result will be a list that is the same as \textit{list} except for the removal of the elements at each given \textit{index}. The result shall be as if all removals happen simultaneously and the order of the \textit{index} arguments shall be unimportant; if an element is indicated twice (whether through syntactically identical indices or not) then it will be as if it was only indicated once.

\section{Examples}
\begingroup\small\begin{verbatim}
% lremove {a b c d e} 1
a c d e
% lremove {a b c d e} end-1
a b c e
% lremove {a b c d e} 1 3
a c e
% lremove {a b c d e} 3 1
a c e
% lremove {a b c d e} 2 2
a b d e
% lremove {a b c d e} 3 end-1
a b c e
% lremove {a b c d e} 1 3 1 4 0
c
\end{verbatim}
\endgroup

\section{Implementation}

\textit{Pending.}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#176]{tip176}
Damon Courtney,
Don Porter,
Damon Courtney,
\emph{Add String Index Values},
on-line at \url{http://purl.org/tcl/tip/176.html}
\end{thebibliography}
\end{document}
