\documentclass{article}

\usepackage{amsmath}

\usepackage{psfig}

\usepackage{fullpage}

\usepackage{6001}

\usepackage{mt}

%\pagestyle{empty}

\raggedright
\raggedbottom

\newcommand{\meta}[1]{\ensuremath{\langle\mathrm{#1}\rangle}}

\makeatletter

\def\@texttop{\vskip \z@ plus1fil\relax}

\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\centering\normalfont\fontsize{24}{28pt}\bfseries}}

\def\ps@sheadings{%
    \let\@oddfoot\@empty\let\@evenfoot\@empty
    \def\@evenhead{\hfil{\thepage}}%
    \def\@oddhead{\hfil{\thepage}}%
    \let\@mkboth\@gobbletwo
    \let\sectionmark\@gobble
    \let\subsectionmark\@gobble
    }

\makeatother

\pagestyle{sheadings}

\setcounter{secnumdepth}{0}

\newcommand{\evalsto}{\ensuremath{\Longrightarrow}}

\newcommand{\T}{\Code{\#t}}
\newcommand{\F}{\Code{\#f}}

\begin{document}

\makeatletter
\def\@listi{\leftmargin\leftmargini
            \parsep 5\p@  \@plus2.5\p@ \@minus\p@
            \topsep 10\p@ \@plus4\p@   \@minus6\p@
            \itemsep15\p@  \@plus2.5\p@ \@minus\p@}

\makeatother

\begin{center}

\fontsize{24}{28pt}\selectfont

Scheming Recursively\\[2\bigskipamount]

\fontsize{20}{28pt}\selectfont

Lecturer: Professor Albert R. Meyer\\[3\bigskipamount]

September 9, 1997

\end{center}

\newpage

\fontsize{20}{28pt}\selectfont

Scheme programming focuses on \emph{Values}:
\begin{enumerate}

\item
Numbers,\\
written as \quad
0,\; 1,\; -2.7,\; 987e11, \dots

\item
Truth Values (Booleans),\\
written as \quad
\texttt{\#t},\; \texttt{\#f}.

\end{enumerate}

\dots that's all for today.

\newpage

The value of the \emph{Expression}
\begin{equation*}
\Code{(+ (* 2 3 4) (- 9 (+ 7 5)) 7.1)}
\end{equation*}
is the number 28.1.  % No it isn't!

\begin{equation*}
\underbrace{\Code{(+ (* 2 3 4) (- 9 (+ 7 5)) 7.1)}}_{\textstyle\mathrm{Expression}} \evalsto
\underbrace{28.1}_{\textstyle\mathrm{Value}}
\end{equation*}

The value of the expression \Code{(< 2 3.7 5)} is true.

\begin{equation*}
\Code{(< 2 3.7 5)} \evalsto \Code{\#t}
\end{equation*}

\newpage

\section{Expression}

A \emph{combination} looks like:
\[\bnf
    (<operator>\ <argument_1>\ <argument_2> \dots)
\]
where $\bnf <operator>$, $\bnf <argument_1>$, \dots are themselves
expressions.

\section{Value Mantra for Combinations:}

``The value of a combination is the result of APPLYing the
\emph{value} of the $\bnf <operator>$ to the \emph{values} of the
$\bnf <argument>$'s.''

\newpage

\section{Value Mantra for Primitive Applications}

\begin{enumerate}

\item[``1.]
The value of operator \Code{+} applied to number values is the sum of the
numbers.

\item[2.]
The value of op \Code{*} applied to numbers is the product of
the numbers.

\item[3.]
The value of op \Code{<} applied to numbers is \T\ if the
numbers are in strictly increasing order; it is \F\ otherwise.

\newpage

\item[4.]
The value of op \Code{-} applied to two numbers is their
difference.  Op~\Code{-} applied to other than two numbers is somewhat
arbitrary (look it up if you're worried about it).

\end{enumerate}
and so on\dots''

\newpage

\section{Expression as a tree}

\centerline{\psfig{file=tree.ps}}

\bigskip

\centerline{\Code{(+ (* 2 3 4) (- 9 (+ 7 5)) 7.1)}}

\newpage

\section{IF expressions}

\begin{align*}
\Code{(if \T\ 1 2)} & \evalsto 1\\
\Code{(if \F\ 1 2)} & \evalsto 2\\
\Code{(if (< 3 2) 0 (> 9 8 7))} & \evalsto \T
\end{align*}

\bigskip

\begin{center}
\Code{(if $\bnf <predicate>$ $\bnf <consequent>$ $\bnf <alternative>$)}
\end{center}

\section{Value Mantra for IF}

``When the value of $\bnf <predicate>$ is~\F, the value of the
if-expression is the value of the $\bnf <alternative>$; when the value of
$\bnf <predicate>$ is not~\F, then the value of the if-expression is the
value of the $\bnf <consequent>$.''

\newpage

What is the value of
\[
    \Code{(+ x 1)}?
\]

What is the value of \Code{x}?

--- Depends how \Code{x} is defined:

\begin{lisp}
(define x 3)

(+ x 1) $\evalsto$ 4


(define x -1)

(+ x 1) $\evalsto$ 0
\end{lisp}

\newpage

A VARIABLE is an expression:

\[
    \Code{x}\qquad \Code{xy} \qquad \Code{xplusy} \qquad \Code{x.1.y}
\]

A set of definitions of the values of variables is called an
\emph{environment}.

\section{Value Mantra for Variables}

``The value of a variable in an environment is whatever the
environment defines it to be.''

\newpage

``At `top-level' of REPL:
\[
    \Code{(define $\bnf<variable>$ $\bnf<expression>$)}
\]
causes the value of $\bnf<variable>$ to be the value of $\bnf
<expression>$ in the top-level, `global' environment.''

This is the \textbf{Global Define Mantra}.

Technicality:\\
We don't ask or care about the value of \Code{(define x 3)}.

\newpage

\section{Compound procedures}

\begin{lisp}
(define (add1 x)
    (+ x 1))
\end{lisp}

adds a compound procedure named \texttt{add1} to the global
environment.

Now

\begin{align*}
\Code{(add1 2)} & \evalsto 3\\
\Code{(add1 7)} & \evalsto 8
\end{align*}

\newpage


\section{Value Mantra for Compound Procedure Application}

``The value of a compound procedure named \texttt{proc} applied to a
value, $n$, in an environment where \texttt{proc} is defined by
\[
    \Code{(define (proc x) $\bnf<expression>$)}
\]
is the value of \meta{expression} in the environment with \Code{x}
given the value~$n$.''

\newpage

\begin{lisp}
(define (square x)
    (* x x))
\end{lisp}

Value of \Code{(square 3)} is the value of
\[
    \Code{(* x x)}
\]
in an environment with \Code{x} given the value~3.\\
(Compound Procedure Apply Mantra)

\newpage

So Value of \Code{x} is 3.  (Variable mantra)

So Value of \Code{(* x x)} is result of

Applying the value of \Code{*} to 3 and 3 (Combination Mantra),

which is the product of 3 and~3, namely~9\\
(Primitive Application Mantra).

\newpage

\section{Sum of Squares}

Let $s(n) = 0^2 + 1^2 + 2^2 + \dots + n^2$

\begin{flalign*}
\mathrm{So} && s(0) &= 0,  & s(1) &= 1,  & s(2) = 5, \\
            && s(3) &= 14, & s(4) &= 30, & \dots
\end{flalign*}

Useful fact:
\[
    s(n) = \underbrace{0^2 + 1^2 + 2^2 + \dots + (n-1)^2}_{\textstyle s(n-1)}
            {} + n^2
\]

\begin{enumerate}

\item
We know $s(0) = 0$.

\item
Useful fact tells us how to find the value of $s(n)$ \emph{assuming}
we know $s(n-1)$.  \emph{Wishful thinking} is ok here!

\end{enumerate}

\newpage

\section{In Scheme}

\begin{lisp}
(define (sum-sq n)
  (if (zero? n)
      0
      (+ (sum-sq (dec n))
         (square n))))
\end{lisp}

Example:
\[
    \Code{(sum-sq 3)} \evalsto s(3), \text{ namely 14.}
\]

\newpage

\section{Using the Mantras Again}

Val of \Code{(sum-sq 3)} is

Val of 
\begin{lisp}
  (if (zero? n)
      0
      (+ (sum-sq (dec n))
         (square n)))
\end{lisp}
when Val of $n$ is 3.

Val of \Code{(zero? n)} when \Code{n} is 3, is \F.

So Val of \Code{(if \dots)} above is Val of
\begin{lisp}
(+ (sum-sq (dec n))
   (square n))
\end{lisp}

\newpage

Val of \Code{(square n)} when \Code{n} is 3 is 9.

Val of \Code{(dec n)} is 2.

So Val of \Code{(sum-sq 3)} is the sum of 9 and the Val of
\Code{(sum-sq 2)}.

\centerline{\vdots}

\[
    9 + 4 + 1 + 0 = 14
\]

\newpage

How to tell if an integer~$n$ is odd?

\emph{Wishful thinking}, assume we could tell if $n-1$ is odd.  Then
$n$ is odd iff $n-1$ is not odd:
\begin{lisp}
(define (odd? n)
  (if (zero? n)
      \#f
      (not (odd? (dec n)))))
\end{lisp}

\newpage

Val of \Code{(odd? 3)} is \\
\hfill[because \Code{(zero? 3)} \evalsto \F]

Val of \Code{(not (odd? 2))}, which is

Val of \Code{(not (not (odd? 1)))}, which is

Val of \Code{(not (not (not (odd? 0))))}, which is

Val of \Code{(not (not (not #f)))}, which is

\T

\newpage

\section{Making More than One Wish}

\begin{lisp}
(define (odd? n)
  (if (zero? n)
      \#f
      (even? (dec n))))

(define (even? n)
  (if (zero? n)
      \#t
      (odd? (dec n))))
\end{lisp}

\newpage


Val of \Code{(odd? 3)} is

Val of \Code{(even? 2)}, which is

Val of \Code{(odd? 1)}, which is

Val of \Code{(even? 0)}, which is

\T


\newpage

\section{Imperative Style}

\[
    \sum_{i = 0}^n i^2 = 0^2 + 1^2 + \dots + n^2
\]

``Starting with $i = 0$, repeatedly add $i^2$ to the running sum and
increment~$i$ until $i$ exceeds~$n$.''

\newpage

Corresponds to simple ``register machine'' or ``assembly language''
program:

\begin{verbatim}
start:      i := 0;
            s := 0;
loop:       if  i = n + 1  goto end;
            temp := square i;
            i := i + 1;
            s := s + temp;
            goto loop;
end:
\end{verbatim}
at the end the sum of squares is in \Code{s}.

\section{Recursive Style is Just as Powerful}

\begin{lisp}
(define (sum-sq-imp n)
  (loop 0 0 n))

(define (loop i s n)
  (if (= i (inc n))
      (end s)
      (next i s n (square i))))

(define (next i s n temp)
  (loop (inc i)
        (+ s temp)
        n))
\end{lisp}

\newpage

\section{Another example: \texttt{odd?}/\texttt{even?}}

\begin{verbatim}
start:      if (n = 0) goto end-false;
            n := n - 1;
            if (n = 0) goto end-true;
            n := n -1;
            goto start;
\end{verbatim}

\newpage

\begin{center}

Register machines correspond to a\\
\emph{special case}\\
of recursive procedures called \\
TAIL-RECURSIVE\\
procecures

\end{center}

\end{document}
