1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
\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 \textbf{bind} does not work on procedures with an arbitrary number
of arguments.
\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}
See also: \textbf{procedure} (\ref{labprocedure}), \textbf{proc} (\ref{labproc}), \textbf{function} (\ref{labfunction}), \textbf{@} (\ref{labconcat})
|