\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}{5/7/2004}
{} % {Problem Set 4}
{} % {Instructor: foo}
{Recitation 26} %{Bryan Russell}

%% \section*{Upcoming Deadlines}

%% \begin{itemize}
%% \item Office hours this week: Tuesday 4/13, 3-5pm in 34-501
%% \item Quiz 2 Tuesday, 4/13 7:30pm-9:30pm
%% \item Lecture 18 due Friday, 4/16 at 9am
%% \item No tutorial Monday/Tuesday next week (institute holiday)
%% \end{itemize}

\section*{Adding the AND special form}

Now let's add support for AND to the explicit control evaluator. For
simplicity, we'll only worry about ANDs of two expressions, like
this: 

\begin{verbatim}
  (and e1 e2)
\end{verbatim}

We'll assume that we have primitive operators first-conjunct and
second-conjunct that pull out e1 and e2, respectively, from an AND
expression.

First we'll add a clause to eval-dispatch:

\begin{verbatim}
eval-dispatch
  ...
  (test (op and?) (reg exp))
  (branch (label ev-and))
  ...
\end{verbatim}

%% \begin{enumerate}
1.  Here is the code for {\verb+ev-and+}.  Fill in the blanks.

\begin{verbatim}
ev-and

  (assign unev ________________________________________ )

  (assign exp _________________________________________ )

  (save continue)

  (save env)

  (save unev)

  (assign continue eval-after-first)

  (goto _____________________________________________ )

eval-after-first

  (restore __________________________________________ )

  (restore __________________________________________ )

  (test (op true?) (reg val))

  (branch (label eval-second-arg))

  (assign val (const #f))

  (restore __________________________________________ )

  (goto (reg continue))

eval-second-arg

  (assign exp _________________________________________ )

  (assign continue eval-after-second)

  (goto _____________________________________________ )

eval-after-second

  _____________________________________________________

  (goto (reg continue))
\end{verbatim}

2.  Does this {\verb+ev-and+} routine handle tail recursion?  Consider this
   Scheme code:

\begin{verbatim}
(define (list? x)
  (or (null? x)
      (and (pair? x) (list? (cdr x)))))

(define z (list 1))
(set-cdr! z z)

(list? z)
\end{verbatim}

If you ran this code in MIT Scheme, would you get a stack overflow, or
an infinite loop?  What if you ran it in the explicit control
evaluator using ev-and as implemented above?\\

3.  Make {\verb+ev-and+} tail-recursive.
%% \end{enumerate}

\end{document}
