\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#282: Enhanced Expression Syntax}
\date{October 13, 2006}
\author{Will Duquette, Don Porter}
\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 \#282}&\textbf{Enhanced Expression Syntax}\\\hline
Author&

Will Duquette \tipmail{William.H.Duquette}{jpl.nasa.gov}
\par
Don Porter \tipmail{dgp}{users.sourceforge.net}
\\
Created&Friday, \(\text{13}^{\text{th}}\) October 2006\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Keywords&expr, operator, assignment\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP extends the syntax of the \textbf{expr} command to allow a sequence of mathematical computations to be expressed clearly and concisely.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

The ugliness of mathematical code written in Tcl is a perennial source of complaint. Consider the following computation:
\begingroup\small\begin{verbatim}
    set x [expr {5*$y + 6*$z}]
    set w [expr {$x**2 + $y**2}]
    set v [expr {$w**2 + $y**2}]
\end{verbatim}
\endgroup

The [\textbf{expr \{...\}}] constructs make the code considerably harder to read. But suppose \textbf{expr} syntax included an assignment operator:
\begingroup\small\begin{verbatim}
    expr {x = 5*$y  + 6*$z}
    expr {w = $x**2 + $y**2}
    expr {v = $w**2 + $y**2}
\end{verbatim}
\endgroup

Next, suppose that an \textbf{expr} expression could include subexpressions, delimited by a ``;'' character:
\begingroup\small\begin{verbatim}
    expr {
        x = 5*$y  + 6*$z;
        w = $x**2 + $y**2;
        v = $w**2 + $y**2
    }
\end{verbatim}
\endgroup

The sequence of computations is now much clearer.

\section{Specification}

\subsection{Assignment Operator}

The \textbf{expr} syntax should be extended with an assignment operator which has C-like semantics, i.e., the result of an assignment is the assigned value. This operator shall be written as ``\textbf{=}''. The term on the left side of the operator shall be a variable name, and the term on the right side of the operator shall be any expression. The result of the overall assignment expression shall be the result of the subexpression on the right hand side of the operator.

\subsection{Expression Separator Operator}

An \textbf{expr} expression can be separated into subexpressions by the semicolon (``\textbf{;}'') character, which will have the same semantics as the ``,'' operator in C. Thus, the return value of a call to \textbf{expr} is the value of the final subexpression, and the both sides of the expression shall be expressions.

\section{Reference Implementation}

See [\url{http://sourceforge.net/tracker/?func=detail&aid=1969722&group_id=10894&atid=310894}]

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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