\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}{3/15/2004}
{} % {Problem Set 4}
{} % {Instructor: foo}
{Tutorial 6} %{Bryan Russell}

\section*{Topics}
Tagged Data and Tables

\section*{Upcoming Deadlines}

\begin{itemize}
\item Problem set 5 due Tuesday, 3/16 at midnight
\item Lecture 12 due Wednesday, 3/17 at 9am
\item Lecture 13 LIVE on Thursday, 3/18 at 10am
\item Project 3 due Friday, 3/19 at 6pm
\item Reminder: my office hours are Wednesdays from 3-5pm
\item Next week is spring break (no office hours)
\end{itemize}

\section*{Tables - Binary Tree Implementation}

Some constructors, accessors, and predicates:

\begin{verbatim}
(define (make-empty-node) nil)
(define empty-node? null?)
(define node-tag 'node)
(define (make-node key val)
  (list node-tag key val (make-empty-node) (make-empty-node)))
(define (node? n)
  (and (pair? n) (eq? (car n) node-tag)))
(define get-key cadr)
(define get-val caddr)
(define get-left cadddr)
(define get-right caddddr)
(define (put! node val)
  (set-car! (cdr node) val) node)
(define (put-left! node left-node)
  (set-car! (cdddr node) left-node) node)
(define (put-right! node right-node)
  (set-car! (cddddr node) right-node) node)

(define table-tag 'table)
; comp-proc is a procedure that compares two values x and y.  If 
; (= x y) is true, comp-proc returns 0.  If (< x y) is true, then 
; comp-proc returns -1.  Else, it returns 1.
(define (make-table comp-proc) 
  (list table-tag comp-proc (make-empty-node)))
\end{verbatim}

\subsection*{Exercise 1}
Write the procedure ${\verb+table-get+}$ that gets the value
associated with a ${\verb+key+}$.

\begin{verbatim}
(define (table-get tbl key)
  EXERCISE-1)
\end{verbatim}

\subsection*{Exercise 2}
Fill in the procedure ${\verb+table-put!+}$ that inserts a
${\verb+key, value+}$ pair into the table.

\begin{verbatim}
(define (table-put! tbl key val)
  (define (insert-help node key val comp-proc)
    EXERCISE-2)
  (set-car! (cddr tbl) (insert-help (caddr tbl) key val (cadr tbl))))
\end{verbatim}

\subsection*{Exercise 3}
What is the time complexity for insertion?  How about retrieval?

\section*{Tables - Memoization}

\subsection*{Exercises 4-6}
We wish to store the result of computation on problems of smaller
size for use in future computations (assume that we use a hash table
and that we've defined ${\verb+size+}$ and ${\verb+hashfunc+}$
elsewhere).  Use this procedure to memoize ${\verb+fib+}$.  What is
the time and space complexity of ${\verb+memo-fib+}$?

\begin{verbatim}
(define (memoize f)
  (let ((table (make-table size hashfunc)))
    (lambda (x)
      (let ((previously-computed-result EXERCISE-4))
        (or previously-computed-result
            (let ((result EXERCISE-5))
              EXERCISE-6
              result))))))

(define memo-fib
  EXERCISE-7)
\end{verbatim}

\end{document}
