\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#331: Allow [lset] to Extend Lists}
\date{September 22, 2008}
\author{Kevin B. Kenny}
\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 \#331}&\textbf{Allow [lset] to Extend Lists}\\\hline
Author&

Kevin B. Kenny \tipmail{kennykb}{acm.org}
\\
Created&Monday, \(\text{22}^{\text{nd}}\) September 2008\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.5 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to modify the \textbf{lset} command to allow it to extend lists.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Proposal}

The \textbf{lset} command shall be modified to allow the index \textit{end+1} (or any equivalent index designating the element one beyond the last element of the list). For the simple usage:
\begingroup\small\begin{verbatim}
    lset list end+1 foo
\end{verbatim}
\endgroup

the effect is the same as if the script had evaluated the command:
\begingroup\small\begin{verbatim}
    lappend list foo
\end{verbatim}
\endgroup

For the usage:
\begingroup\small\begin{verbatim}
    lset list $n end+1 foo
\end{verbatim}
\endgroup

the effect is much the same as if the script had evaluated:
\begingroup\small\begin{verbatim}
    set temp [lindex $list $n]
    lappend temp foo
    lset list $n $temp
\end{verbatim}
\endgroup

(except, of course that no \textit{temp} variable is created)

The usage:
\begingroup\small\begin{verbatim}
    lset list end+1 0 foo
\end{verbatim}
\endgroup

or, equivalently,
\begingroup\small\begin{verbatim}
    lset list end+1 end+1 foo
\end{verbatim}
\endgroup

is equivalent to
\begingroup\small\begin{verbatim}
    lappend list [list foo]
\end{verbatim}
\endgroup

\section{Rationale}

Clearly, there are equivalent constructs to all of these usages. Nevertheless, it is convenient not to have to worry about whether a given index is in bounds, particularly when iterating through a vector or matrix. With the proposed change, many of the standard algorithms in linear algebra will just work, without either having to preinitialize a list to a given size or to have a test to determine whether to use \textbf{lappend} or \textbf{lset} to store a given element.

\section{Alternatives}

Explicitly out of scope is the treatment of indices greater than or equal to \textit{end+2}. Setting non-contiguous elements would raise false expectations of sparse lists, \textbf{NULL} elements, and so on.

\section{Copyright}

Copyright (c) 2008 by Kevin B. Kenny.

This document may be distributed subject to the terms and conditions set forth in the Open Publication License, version 1.0 [\url{http://www.opencontent.org/openpub/}].

\section{Colophon}

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