% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\MappingPackageOneXmpTitle}{MappingPackage1}
\newcommand{\MappingPackageOneXmpNumber}{9.55}
%
% =====================================================================
\begin{page}{MappingPackageOneXmpPage}{9.55 MappingPackage1}
% =====================================================================
\beginscroll

%
%
%
%
%



Function are objects of type \pspadtype{Mapping}.
In this section we demonstrate some library operations from the
packages \spadtype{MappingPackage1}, \spadtype{MappingPackage2}, and
\spadtype{MappingPackage3} that manipulate and create functions.
Some terminology: a {\it nullary} function takes no arguments,
%-% \HDindex{function!nullary}{MappingPackageOneXmpPage}{9.55}{MappingPackage1}
a {\it unary} function takes one argument, and
%-% \HDindex{function!unary}{MappingPackageOneXmpPage}{9.55}{MappingPackage1}
a {\it binary} function takes two arguments.
%-% \HDindex{function!binary}{MappingPackageOneXmpPage}{9.55}{MappingPackage1}

\xtc{
We begin by creating an example function that raises a
rational number to an integer exponent.
}{
\spadpaste{power(q: FRAC INT, n: INT): FRAC INT == q^n \bound{power}}
}
\xtc{
}{
\spadpaste{power(2,3) \free{power}}
}
\xtc{
The \spadfunFrom{twist}{MappingPackage3} operation
transposes the arguments of a binary function.
Here \spad{rewop(a, b)} is \spad{power(b, a)}.
}{
\spadpaste{rewop := twist power \free{power}\bound{rewop}}
}
\xtc{
This is \texht{$2^3.$}{\spad{2^3.}}
}{
\spadpaste{rewop(3, 2) \free{rewop}}
}
\xtc{
Now we define \userfun{square} in terms of \userfun{power}.
}{
\spadpaste{square: FRAC INT -> FRAC INT \bound{squaredec}}
}
\xtc{
The \spadfunFrom{curryRight}{MappingPackage3} operation creates a unary
function from a binary one by providing a constant
argument on the right.
}{
\spadpaste{square:= curryRight(power, 2) \free{squaredec poswer}\bound{square}}
}
\xtc{
Likewise, the
\spadfunFrom{curryLeft}{MappingPackage3} operation provides a constant
argument on the left.
}{
\spadpaste{square 4 \free{square}}
}
\xtc{
The \spadfunFrom{constantRight}{MappingPackage3} operation creates
(in a trivial way) a binary function from a unary one:
\spad{constantRight(f)} is the function \spad{g} such that
\spad{g(a,b)= f(a).}
}{
\spadpaste{squirrel:= constantRight(square)\$MAPPKG3(FRAC INT,FRAC INT,FRAC INT) \free{square}\bound{squirrel}}
}
\xtc{
Likewise,
\spad{constantLeft(f)} is the function \spad{g} such that \spad{g(a,b)= f(b).}
}{
\spadpaste{squirrel(1/2, 1/3) \free{squirrel}}
}
\xtc{
The \spadfunFrom{curry}{MappingPackage2} operation makes a unary function nullary.
}{
\spadpaste{sixteen := curry(square, 4/1) \free{square}\bound{sixteen}}
}
\xtc{
}{
\spadpaste{sixteen() \free{sixteen}}
}
\xtc{
The \spadopFrom{*}{MappingPackage3} operation
constructs composed functions.
}{
\spadpaste{square2:=square*square \free{square}\bound{square2}}
}
\xtc{
}{
\spadpaste{square2  3 \free{square2}}
}
\xtc{
Use the \spadopFrom{^}{MappingPackage1} operation to create
functions that are \spad{n}-fold iterations of other functions.
}{
\spadpaste{sc(x: FRAC INT): FRAC INT == x + 1 \bound{sc}}
}
\xtc{
This is a list of \pspadtype{Mapping} objects.
}{
\spadpaste{incfns := [sc^i for i in 0..10] \free{sc}\bound{incfns}}
}
\xtc{
This is a list of applications of those functions.
}{
\spadpaste{[f 4 for f in incfns] \free{incfns}}
}
\xtc{
Use the \spadfunFrom{recur}{MappingPackage1}
operation for recursion:
\spad{g := recur f} means \spad{g(n,x) == f(n,f(n-1,...f(1,x))).}
}{
\spadpaste{times(n:NNI, i:INT):INT == n*i \bound{rdec}}
}
\xtc{
}{
\spadpaste{r := recur(times) \free{rdec}\bound{r}}
}
\xtc{
This is a factorial function.
%-% \HDindex{factorial}{MappingPackageOneXmpPage}{9.55}{MappingPackage1}
}{
\spadpaste{fact := curryRight(r, 1) \free{r}\bound{fact}}
}
\xtc{
}{
\spadpaste{fact 4 \free{fact}}
}
\xtc{
Constructed functions can be used within other functions.
}{
\begin{spadsrc}[\free{square}\bound{mto2ton}]
mto2ton(m, n) ==
  raiser := square^n
  raiser m
\end{spadsrc}
}
\xtc{
This is \texht{$3^{2^3}.$}{\spad{3^(2^3).}}
}{
\spadpaste{mto2ton(3, 3) \free{mto2ton}}
}
\xtc{
Here \userfun{shiftfib} is a unary function that modifies its argument.
}{
\begin{spadsrc}[\bound{shiftfib}]
shiftfib(r: List INT) : INT ==
  t := r.1
  r.1 := r.2
  r.2 := r.2 + t
  t
\end{spadsrc}
}
\xtc{
By currying over the argument we get a function with private state.
}{
\spadpaste{fibinit: List INT := [0, 1] \bound{fibinitdec}}
}
\xtc{
}{
\spadpaste{fibs := curry(shiftfib, fibinit) \free{shiftfib fibinit}\bound{fibs}}
}
\xtc{
}{
\spadpaste{[fibs() for i in 0..30] \free{fibs}}
}
\endscroll
\autobuttons
\end{page}
%
