\subsection{bind}
\label{labbind}
\noindent Name: \textbf{bind}\\
\phantom{aaa}partially applies a procedure to an argument, returning a procedure with one argument less\\[0.2cm]
\noindent Usage: 
\begin{center}
\textbf{bind}(\emph{proc}, \emph{ident}, \emph{obj}) : (\textsf{procedure}, \textsf{identifier type}, \textsf{any type}) $\rightarrow$ \textsf{procedure}\\
\end{center}
Parameters: 
\begin{itemize}
\item \emph{proc} is a procedure to be partially applied to an argument
\item \emph{ident} is one of the formal arguments of \emph{proc}
\item \emph{obj} is any \sollya object \emph{ident} is to be bound to
\end{itemize}
\noindent Description: \begin{itemize}

\item \textbf{bind} allows a formal parameter \emph{ident} of a procedure \emph{proc} to 
   be bound to an object \emph{obj}, hence \emph{proc} to be partially applied.
   The result of this curryfied application, returned by \textbf{bind}, is 
   a procedure with one argument less. This way, \textbf{bind} permits 
   specialization of a generic procedure, parameterized e.g. by a function
   or range.

\item In the case when \emph{proc} does not have a formal parameter named 
   \emph{ident}, \textbf{bind} prints a warning and returns the procedure 
   \emph{proc} unmodified. 

\item \textbf{bind} always returns a procedure, even if \emph{proc} only has one 
   argument, which gets bound to \emph{ident}. In this case, \textbf{bind}
   returns a procedure which does not take any argument. Hence 
   evaluation, which might provoke side effects, is only performed
   once the procedure gets used.

\item If \textbf{bind} is used on a procedure with an arbitrary number of
   arguments, no check is performed whether the procedure actually
   has a parameter of name \emph{ident}. \textbf{bind} returns a procedure
   with an arbitrary number of arguments whose argument has the
   same name as the original procedure \emph{proc}. That returned procedure
   binds \emph{ident} to \emph{obj} and then runs \emph{proc} within this context,
   applying it to its original argument. The execution of \textbf{bind}
   fails for procedures with an arbitrary number of arguments if
   the original procedure's argument has the same name as \emph{ident}.
   In this case, the procedure is returned unmodified.
\end{itemize}
\noindent Example 1: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> procedure add(X,Y) { return X + Y; };
> succ = bind(add,X,1);
> succ(5);
6
> succ;
proc(Y)
{
nop;
return (proc(X, Y)
{
nop;
return (X) + (Y);
})(1, Y);
}
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 2: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> procedure add(X,Y) { return X + Y; };
> succ = bind(add,X,1);
> five = bind(succ,Y,4);
> five();
5
> five;
proc()
{
nop;
return (proc(Y)
{
nop;
return (proc(X, Y)
{
nop;
return (X) + (Y);
})(1, Y);
})(4);
}
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 3: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> verbosity = 1!;
> procedure add(X,Y) { return X + Y; };
> foo = bind(add,R,1);
Warning: the given procedure has no argument named "R". The procedure is returne
d unchanged.
> foo;
proc(X, Y)
{
nop;
return (X) + (Y);
}
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 4: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> procedure sum_times(l=...) { var s, i; s = 0; for i in l do s = s + i; return 
c * s; };
> foo = bind(sum_times, c, 17);
> foo;
proc(l = ...)
{
var c;
c = 17;
return (proc(l = ...)
{
var s, i;
s = 0;
for i in l do
s = (s) + (i);
return (c) * (s);
}) @ (l);
}
> foo(1, 2, 3);
102
\end{Verbatim}
\end{minipage}\end{center}
See also: \textbf{procedure} (\ref{labprocedure}), \textbf{proc} (\ref{labproc}), \textbf{function} (\ref{labfunction}), \textbf{@} (\ref{labconcat})
