\input 6001mac.tex

\def\answer#1{{\cf \color{white} #1}}
% \def\answer#1{{\cf\color{red} #1}}

\pagestyle{empty}

\begin{document}

\noindent
\makebox[1.95in][l]{March 5, 1999}
\makebox[3in][c]{\LARGE 6.001 Recitation Notes}
\makebox[1.95in][r]{Brian Scassellati}

\noindent\rule[0pt]{7in}{.02in}

\section{Symbols and Quote}

Scheme uses the character {\cf '} to distinguish between symbols and
their values.  The value of {\cf 'x} is the symbol {\cf x} and not the
value of the thing named {\cf x}.  

The quote syntax {\cf 'expr} is interpreted by the reader as syntactic
sugar for {\cf (quote expr)}.

Practice giving the printed representation and the box-and-pointer
diagram for each of the following:

\hspace{.25in}\begin{tabular}[t]{|p{3in}|p{3in}|}\hline
\bl
\vspace{.5pc}
'(a b)
\RA \answer{(a b)}
\el
\vspace{.5in}%\centerline{\psfig{file=r10-4.eps,height=.5in,silent=,clip=}}
&
\bl
\vspace{.5pc}
(cons 'a '(b)) 
\RA \answer{(a b)}
\el
\vspace{.5in}%\centerline{\psfig{file=r10-1.eps,height=.5in,silent=,clip=}}
\\ \hline
\bl
\vspace{.5pc}
(list 'a 'b)
\RA \answer{(a b)}
\el
\vspace{.5in}%\centerline{\psfig{file=r10-5.eps,height=.5in,silent=,clip=}}
&
\bl
\vspace{.5pc}
(quote (testing one two))
\RA \answer{(testing one two)}
\el
\vspace{.5in}%\centerline{\psfig{file=r10-6.eps,height=.5in,silent=,clip=}}
\\ \hline
\bl
\vspace{.5pc}
(list '(a) '(b))
\RA \answer{((a) (b))}
\el
\vspace{.5in}%\centerline{\psfig{file=r10-2.eps,height=.75in,silent=,clip=}}
&
\bl
\vspace{.5pc}
(cons '(a) '(b))
\RA \answer{((a) b)}
\el
\vspace{.5in}%\centerline{\psfig{file=r10-3.eps,height=.75in,silent=,clip=}}
\\ \hline
\end{tabular}
\vspace{1pc}

Given the definitions above the line below, what does the Scheme
interpreter print for each of the expressions below the line?
\vspace{.5pc}

\bl
\hspace{.25in}\begin{tabular}[t]{p{2in}p{2in}p{2.5in}}
(define a 3)&
(define p (list cons +))&
(define x '(a b))\\
(define b 4)&
(define q '(cons +))&
(define alpha '(a Greek letter))\\
(define c (list 5 6))&
(define r (list 'cons '+))&
(define beta '(I don't know))\\
\end{tabular}
\el
\rule[6pt]{7in}{.01in}

\bl
\begin{tabular}[t]{p{2in}p{2in}p{2.5in}}
(list 'a b)
\RA \answer{(a 4)}
& 
(car x)
\RA \answer{a}
&
((car r) 3 4)
\RA \answer{Error -- can't apply a symbol}
\\
'(a b)
\RA \answer{(a b)}
&
(cdr alpha)
\RA \answer{(Greek letter)}
&
((cadr q) 3 4)
\RA \answer{Error -- can't apply a symbol}
\\
(cons b x)
\RA \answer{(4 a b)}
& 
((car p) 3 4)
\RA \answer{(3 . 4)}
&
(car ''a)
\RA \answer{quote}
\\
(list 'b c)
\RA \answer{(b (5 6))}
&
((cadr p) 3 4)
\RA \answer{7}
& 
beta
\RA \answer{(I don (quote t) know)}
\\
\end{tabular}
\el

\section{Symbol Equality}

To determine if two numbers are identical, we use the Scheme primitive
{\cf =}.  To determine if two symbols are identical, we use the Scheme
primitive {\cf eq?}.  For example,

\begin{tabular}[t]{p{2in}p{2in}}
\bl
(eq? 'apples 'apples)
\RA \#t
\el
&
\bl
(eq? 'apples 'oranges)
\RA \#f
\el
\end{tabular}

We will discuss other variations of {\cf eq?} and other equality
predicates next week, but remember that the behavior of {\cf eq?} on
numerical arguments is {\bf unspecified}:

\begin{tabular}[t]{p{2in}p{2in}}
\bl
(eq? 1 1)
\RA unspecified
\vspace{1pc}
\el
&
\bl
(let ((x (+ 1 2)))
  (eq? x x))
\RA unspecified
\el
\end{tabular}

\section{Building memq}

{\cf (memq <item> <list>)} will return return a sublist beginning with
the first occurrence of the symbol {\cf <item>}.  If the symbol {\cf
<item>} is not contained in the {\cf <list>}, then {\cf memq} will
return false.

\begin{tabular}[t]{p{7in}}
\bl
(define (memq item lst)
\answer{
  (cond ((null? lst) false)
        ((eq? item (car lst)) lst)
        (else (memq item (cdr lst)))))
}
\el
\end{tabular}

What is the result of using {\cf memq} in the following situations:

\begin{tabular}[t]{p{3in}p{3in}}
\bl
(memq 'robots '(building robots is fun))
\RA \answer{(robots is fun)}
(memq 'robot '(building robots is fun))
\RA \answer{false}
\el
&
\bl
(memq 'bolts '(screws (bolts washers) nails))
\RA \answer{false}
(memq 'bolts '(screws (bolts washers) nails bolts pins))
\RA \answer{(bolts pins)}
\el
\end{tabular}

\section{Puzzle}

Can you write a scheme expression that prints itself?

(Hint: Consider how a cell reproduces.  It contains its own blueprint
(the DNA strand in the nucleus); reproduction involves (a) copying the
blueprint, (b) implementing the blueprint.  That is, it uses the
blueprint twice.)

\bl
\begin{tabular}[t]{p{8in}}
\answer{
((lambda (x)
   (list x (list (quote quote) x)))
 (quote (lambda (x)
          (list x (list (quote quote) x)))))
}
\end{tabular}
\el

\section{Administrivia}
\begin{itemize}
\itemx Quiz \#1 Statistics:
\begin{itemize}
\itemx Average: 76
\itemx Approximate ranges: A(100-85) B(84-69) C(68-53) D/F(52-0)
\end{itemize}
\itemx Problem Set \#3 : Due next Wednesday
\end{itemize}

\end{document}
