\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#303: Enhance 'llength' Command to Support Nested Lists}
\date{January 29, 2007}
\author{Wolf-Dieter Busch}
\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 \#303}&\textbf{Enhance 'llength' Command to Support Nested Lists}\\\hline
Author&

Wolf-Dieter Busch \tipmail{wolf-dieter.busch}{gmx.de}
\\
Created&Monday, \(\text{29}^{\text{th}}\) January 2007\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.3 $\\
Keywords&Tcl, lindex\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

The command \textbf{llength} currently returns the length of a list. Sometimes it is desirable to know the length of a nested list. This TIP proposes an improvement to \textbf{llength} to do that.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Description}

Currently, finding the length of a nested list requires the combination of \textbf{llength} and \textbf{lindex}. This works, but is not very clean for a comparatively common operation when you compare with straight \textbf{lindex} usage. This TIP proposes to enhance \textbf{llength} so that it also does the indexing, making it's usage in such situations cleaner and less subject to programming errors.

\subsection{Proposed Change}

The \textbf{llength} command shall be updated to have syntax like this:

\begin{quote}
\textbf{llength} \textit{list} ?\textit{indexList} ...?
\end{quote}

When no \textit{indexList} is supplied, the current behavior is used. When one or more \textit{indexList} arguments are supplied, they are used to restrict which part of \textit{list} is taken the length of, just as if \textbf{lindex} had been used to index into \textit{list}.

Thus, [\textbf{llength \{a \{b c\} d\}}] shall return 3 as nowadays, [\textbf{llength \{a \{b c\} d\} 1}]\tiprangle{} shall return the length of the nested list on index 1, here \{b c\} =\tiprangle{} 2, and [\textbf{llength \{a \{b c\} d\} 1 0}] shall return the length of the nested list on index 0 of nested list on index 1, i.e., 1 in this case.

\subsection{Compatibility}

As this only changes a way of use of the \textbf{llength} that currently returns an error, there are no compatibility problems.

\section{Example Implementation}

The procedure \textbf{idxllength} (below) does what is described above, but inefficiently:
\begingroup\small\begin{verbatim}
proc idxllength args {
    llength [lindex {*}$args]
}
\end{verbatim}
\endgroup

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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