\iffalse

INSTRUCTIONS: (if this is not ps1, use the right file name)

  Clip out the ********* INSERT HERE ********* bits below and insert
appropriate TeX code.  Once you are done with your file, run

  ``latex template.tex''

from the Athena shell.  If your TeX code is clean, the latex will exit
back to a prompt.  Once this is done, run

  ``dvips template.dvi -o template.ps''

which should print your file to the nearest printer.  There will be
residual files called ps1.log, ps1.aux, and ps1.dvi.  All these can be
deleted, but do not delete ps1.tex.

\fi
\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{latexsym}
\usepackage{graphicx}
\setlength{\oddsidemargin}{.25in}
\setlength{\evensidemargin}{.25in}
\setlength{\textwidth}{6in}
\setlength{\topmargin}{-0.4in}
\setlength{\textheight}{8.5in}
\setlength{\parindent}{0pt}
%\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}

\newcommand{\var}[0]{
  \textrm{var}
}

\newcommand{\E}[0]{
  \mathbf{E}
}

\newcommand{\cov}[0]{
  \textrm{cov}
}

\newcommand{\handout}[5]{
   \renewcommand{\thepage}{\arabic{page}}
   \noindent
   \begin{center}
   \framebox{
      \vbox{
    \hbox to 5.78in { {\bf 6.001 SICP Spring 2004} \hfill #2 }
       \vspace{4mm}
       \hbox to 5.78in { {\Large \hfill #5  \hfill} }
       \vspace{2mm}
       \hbox to 5.78in { {\it #3 \hfill #4} }
      }
   }
   \end{center}
   \vspace*{4mm}
}

\newcommand{\ho}[5]{\handout{#1}{#2}{Instructor:
#3}{Teaching Assistant: #4}{Handout #1: #5}}
\newcommand{\al}{\alpha}
\newcommand{\Z}{\mathbb Z}

\begin{document}
\handout{P1}{2/17/2004}
{} % {Problem Set 4}
{} % {Instructor: foo}
{Tutorial 2} %{Bryan Russell}

\section*{Topics}
Substitution model, recursion, orders of growth

\section*{Upcoming Deadlines}

\begin{itemize}
\item Problem set 2 due Tuesday, 2/17 at midnight
\item Lecture 5 due Wednesday, 2/18 at 9am
\item Project 2 due Friday, 2/27 at 6pm
\end{itemize}

\section*{Substitution Model}

The substitution model is a means for describing how an expression, in
particular a combination, is evaluated.  It should be emphasized that
the steps of this model is not exactly what happens in the computer.
However, it suffices for now in order to understand various issues of
computation.  

Let us begin with an example.  Suppose we have the following
expression:

\begin{verbatim}
((lambda (x) (* 2 x)) 3)
\end{verbatim}

How do we evaluate this?  First, we begin by identifying what type of
expression this is.  Of course, it is a combination.  Next, we
evaluate all of the subexpressions, in any order.  The first
subexpression is the special form lambda with value ``procedure''
(or, as introduced last week, a ``double-bubble'').  The second
subexpression is self-evaluating with value 3.  Next, we apply the
procedure to the values of its arguments.  We can do this explicitly
by first writing down the body of the procedure:

\begin{verbatim}
(* 2   x   )
\end{verbatim}

Next, we substitute the value of the arguments for the parameters of
the procedure.  In this case, we substitute 3 for $\verb+x+$:

\begin{verbatim}
(* 2   3   )
\end{verbatim}

Finally, we continue with the evaluation of the resulting expression.

\section*{Rules of Evaluation}

To summarize, the rules of evaluation are as follows:

\begin{itemize}
  \item If $\bf{self-evaluating}$, return the value.
  \item If a $\bf{name}$, return the value associated with name in
  environment. 
  \item If a $\bf{special \ form}$, do something special (the exception
  to the rules).
  \item If a $\bf{combination}$, then
    \begin{itemize}
      \item $\it{Evaluate}$ all of the subexpressions of the
      combination in any order.
      \item $\it{Apply}$ the operator to the values of the operands
      (arguments) and return result.
	\begin{itemize}
	  \item If the procedure is a $\bf{primitive \ procedure}$, then
	  just do it.
	  \item If the procedure is a $\bf{compound \ procedure}$, then
	    \begin{itemize}
	      \item Copy the body of the procedure.
	      \item Replace each formal parameter with the
	      corresponding actual argument value.
	      \item Evaluate the resulting expression.
	    \end{itemize}
	\end{itemize}
    \end{itemize}
\end{itemize}

\section*{Recursion and Orders of Growth}

\subsection*{Problem 1 - The Towers of Hanoi}

The Towers of Hanoi problem asks how to move a stack of $n$ disks from
the one pole to another pole.  There are three poles in the game and
the disks start off on the first pole stacked on top of each other.
The disks are of different sizes stacked initially in decreasing order
with the smallest disk on top.  You may move only one disk at a time
from one pole to another.  However, you may not stack a larger disk on
top of a smaller disk.  We wish to write a program to print the
sequence of steps to solve this problem.  Suppose we have the
following piece of code defined:

\begin{verbatim}
(define (my-display k from to)
  (display k)
  (display " ")
  (display from)
  (display " ")
  (display to)
  (newline))
\end{verbatim}

First, what does this do?  Now, we wish to use this to solve our problem:

\begin{verbatim}
(define (hanoi n)
  code-goes-here)
\end{verbatim}

%% Here's the main idea of Stop and Copy garbage collection.  First,
%% split memory in half (Working Memory and Copy Memory), and only
%% allocate cons cells from Working Memory.  When Working Memory fills
%% up, stop computation and begin garbage collection: 

%% \begin{enumerate}
%% \item Copy the root pair over to Copy Memory.  In the root's old location
%% in Working Memory, put the ``Broken Heart'' token in the car and its
%% forwarding address (in Copy Memory) in the cdr.

%% \item Starting at the scan pointer, check its car and the cdr. If either
%% is a pointer, look at that location in Working Memory. If it's already
%% been copied (i.e. it has a ``Broken Heart''), update the reference to
%% it at the scan pointer. Otherwise, copy the location to Copy Memory,
%% put the ``Broken Heart'' in the old location, and update the reference
%% to it at scan.  

%% \item Increment scan, and repeat step 2 until scan has reached the end of
%%    Copy Memory. 

%% \item Swap the roles of the Working and Copy Memory. 
%% \end{enumerate}

%% \subsection*{Problem 1}
%% Perform stop and copy with root P8

%% \begin{displaymath}
%% \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}|c|c|c|c|c|c|c|c|c|c|c|}
%% %\begin{array}{|l|l|l|l|l|l|l|l|l|l|l|}
%% \hline
%% \textrm{working memory} & 1 \ & 2 \ & 3 \ & 4 \ & 5 \ & 6 \ & 7 \ & 8
%% \ & 9 \  & 10 \ \\ \hline
%% \textrm{the-cars} & N1 \ & N3 \ & N2 \ & P5 \ & N4 \ & P3 \ & P5 \ &
%% P1 \ & N5 \ & N6 \ \\ 
%% & & & & & & & & & & \\ \hline
%% \textrm{the-cdrs} & P3 \ & E0 \ & P2 \ & E0 \ & E0 \ & E0 \ & P4 \ & P6
%% \ & E0 \ & E0 \ \\ 
%% & & & & & & & & & & \\ \hline
%% \hline
%% \textrm{copy memory} & 1 \ & 2 \ & 3 \ & 4 \ & 5 \ & 6 \ & 7 \ & 8
%% \ & 9 \  & 10 \ \\ \hline
%% \textrm{the-cars} & \ & \ & \ & \ & \ & \ & \ & \ & \ & \ \\ \hline
%% \textrm{the-cdrs} & \ & \ & \ & \ & \ & \ & \ & \ & \ & \ \\ \hline
%% \end{tabular*}
%% \end{displaymath}

%% \section*{Garbage Collection: Mark-Sweep}

%% Here's the main idea of Mark-Sweep garbage collection.

%% \subsection*{Mark}

%% \begin{enumerate}
%% \item Start at the root and follow the accessible structure (keeping a
%%    stack of where you still need to go). 

%% \item Mark every cell you visit. 

%% \item When you see an already-marked cell, don't follow its car or cdr,
%% in order to avoid going into a cycle. 
%% \end{enumerate}

%% \subsection*{Sweep}

%% \begin{enumerate}
%% \item Start at the beginning of memory and scan each cell. 
%% \item If a cell is unmarked, then it's garbage, so hook it into the free
%% list.
%% \end{enumerate}

%% \subsection*{Problem 2}
%% Perform mark-sweep with root P8

%% \begin{displaymath}
%% \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}|c|c|c|c|c|c|c|c|c|c|c|}
%% \hline
%% \textrm{working memory} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8
%% & 9 & 10 \\ \hline
%% \textrm{the-cars} & N1 & N3 & N2 & P5 & N4 & P3 & P5 &
%% P1 & N5 & N6 \\
%% & & & & & & & & & & \\ \hline
%% \textrm{the-cdrs} & P3 & E0 & P2 & E0 & E0 & E0 & P4 & P6
%% & E0 & E0 \\
%% & & & & & & & & & & \\ \hline
%% \textrm{the marks} & & & & & & & & & & \\ \hline
%% \end{tabular*}
%% \end{displaymath}

%% \section*{Explicit Control Evaluator}
%% What is the Explicit Control Evaluator?  Where does code for new
%% special forms go?

%% \subsection*{Problem 3}
%% Rewrite {\bf while} in the EC Evaluator.  Fill in the blanks below.\\

%% \verb+ev-while+ \\
%% \verb+ (save continue)+ \\
%% \verb+ (save env)+ \\
%% \verb+ (save exp)+ \\
%% \verb+ (assign + \makebox[1in]{\hrulefill}
%% \verb+(op get-pred)+ \makebox[1in]{\hrulefill} \verb+)+ \\
%% \verb+ (assign + \makebox[1in]{\hrulefill} \verb+ (label eval-after-first))+ \\
%% \verb+ (goto + \makebox[1in]{\hrulefill} \verb+)+\\
%% \verb+eval-after-first+\\
%% \verb+ (restore exp)+\\
%% \verb+ (restore env)+\\
%% \verb+ (test (op false?) + \makebox[1in]{\hrulefill} \verb+)+\\
%% \verb+ (branch (label finished))+\\
%% \verb+ (save env)+\\
%% \verb+ (save exp)+\\
%% \verb+ (assign + \makebox[1in]{\hrulefill} \verb+ (op get-body) +
%% \makebox[1in]{\hrulefill} \verb+)+\\
%% \verb+ (assign + \makebox[1in]{\hrulefill} \verb+ (label eval-after-second))+\\
%% \verb+ (goto + \makebox[1in]{\hrulefill} \verb+)+\\
%% \verb+eval-after-second+\\
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+finished+
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\

%% \subsection*{Problem 4}
%% Rewrite {\bf cond} in the EC Evaluator.  Fill in the blanks below.\\

%% \verb+ev-cond+\\
%% \verb+ (test (op empty-cond?) +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (branch (label b-case))+\\
%% \verb+ (save continue)+\\
%% \verb+ (save env)+\\
%% \verb+ (save exp)+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill} \verb+ (op get-first-pred) +
%% \makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill} \verb+ (label eval-after-first))+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+eval-after-first+\\
%% \verb+ (restore exp)+\\
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (test (op true?) +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (branch (label eval-true))+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill}\verb+ (op get-rest) +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill}\verb+ (label eval-after-second))+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+eval-after-second+\\
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+eval-true+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill}\verb+ (op get-first-body) +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill}\verb+ (label eval-true-after-first))+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+eval-true-after-first+\\
%% \verb+ (restore +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\
%% \verb+b-case+\\
%% \verb+ (assign +\makebox[1in]{\hrulefill}\verb+ (const undefined))+\\
%% \verb+ (goto +\makebox[1in]{\hrulefill}\verb+)+\\

%% Is this tail-recursive?  If so, why?  If not, how can we can we change
%% it to make it tail-recursive?

\end{document}
