% -*- Mode: Tex -*-

\def\AnswerSheet{1}

\input 6001mac.tex

\begin{document}

\psetheader{Spring Semester, 1999}{Recitation -- Wednesday, February 3}

{\Large\bf 0. Announcements}

\begin{itemize}
\itemx 
\begin{tabular}{lllll}
{\bf Section Staff:} &
RI: &  Mike Leventon & {\tt leventon@ai.mit.edu} & \\
& TA: & Sandia Ren & {\tt sren@mit.edu} & \\
& TA: & Hooman Vassef & {\tt hvassef@mit.edu} & (Half of section 12)
\end{tabular}

\itemx {\bf Collaboration Policy:}  Read carefully in the handout.  

\itemx {\bf Attendance:}
	Lecture recommended.  Section and Tutorials required.

\itemx {\bf Problem Sets:} 
	No late problem sets accepted.  Psets are an important part of your
grade.  Start them early!
	
\itemx {\bf Course Web Page:}  {\tt http://mit.edu/6.001}

\itemx {\bf Section Web Page:} 	
	{\tt http://www.ai.mit.edu/people/leventon/6001} \\
	Section notes (with solutions) will be posted here.

\end{itemize}

\vspace{0.1in}
{\Large\bf 1. Rules for Scheme}

{\bf To evaluate an expression}, follow these rules:
\begin{itemize}
\itemx A {\bf numeral} evaluates to the {\bf number}   
\itemx A {\bf name} evaluates to {\bf the value associated with that name}
\itemx A {\bf lambda expression} evaluates to a {\bf procedure object}
\itemx A {\bf combination} is evaulated as follows:
  \begin{enumerate}
    \itemx {\bf Evalute} the {\em subexpressions in any order}
    \item {\bf Apply} the {\em value of the operator subexpression} to the 
	{\em values of the remaining subexpressions}.
  \end{enumerate}
\end{itemize}

{\bf To apply a procedure to its arguments}, evaluate the body of the
procedure where each parameter is replaced by the corresponding value.

{\bf To evaluate a define}, evaluate the body of the define, and associate
that value with the name listed in the define.

\vspace{0.1in}
{\Large\bf 2. Simple Examples}

To what do the following expressions evaluate (assume they are evaluated in
sequence)?

\beginlisp
$\Rightarrow$ 7  
\ANSWER{\tt ;Value: 7}
\endlisp

\beginlisp
$\Rightarrow$ -
\ANSWER{\tt ;Value: \#[compiled-procedure ..]}
\endlisp

\beginlisp
$\Rightarrow$ (+ 2 4)
\ANSWER{\tt ;Value: 6}
\endlisp

\beginlisp
$\Rightarrow$ (* (- 5 3) (/ 9 3))
\ANSWER{\tt ;Value: 6}
\endlisp

\beginlisp
$\Rightarrow$ (7 - 4)
\ANSWER{\tt ;Error: The object 7 is not applicable}
\endlisp

\newpage
{\Large\bf 3. More Examples}

To what do the following expressions evaluate (assume they are evaluated in
sequence)?

\begin{tabular}{p{3.5in}|p{3.5in}}
{
\beginlisp
$\Rightarrow$ (lambda (x) (* x x))
\ANSWER{\tt ;Value: \#[compound-procedure ...]}
\endlisp

\beginlisp
$\Rightarrow$ ((lambda (x) (* x x)) 5)
\ANSWER{\tt ;Value: 25}
\endlisp

\beginlisp
$\Rightarrow$ (define double (lambda (x) (* 2 x)))
\ANSWER{\tt ;Value: "double --> \#[compound-procedure ...]}
\endlisp

\beginlisp
$\Rightarrow$ (double (double 6))
\ANSWER{\tt ;Value: 24}
\endlisp

\beginlisp
$\Rightarrow$ (double double)
\ANSWER{\tt ;Error: \#[compound-procedure] passed to multiply}
\endlisp
} & {
\beginlisp
$\Rightarrow$ (define cube (lambda (x) (*x x x)))
\ANSWER{\tt ;Value: "cube --> \#[compound-procedure ...]}
\endlisp

\beginlisp
$\Rightarrow$ (cube 3)
\ANSWER{\tt ;Error: "Unbound variable: *x"}
\endlisp

\beginlisp
$\Rightarrow$ (define + 3)
\ANSWER{\tt ;Value: "+ --> 3"}
\endlisp

\beginlisp
$\Rightarrow$ (define - 6)
\ANSWER{\tt ;Value: "- --> 6"}
\endlisp

\beginlisp
$\Rightarrow$ (* + -)
\ANSWER{\tt ;Value: 18}
\endlisp
}
\end{tabular}

\vspace{0.1in}
{\Large\bf 4. Writing a Procedure}

Define a procedure called {\tt average} that computes the average of its two
numeric arguments.

\beginlisp
\ANSWER{
(define average 
  (lambda (x y) 
    (/ (+ x y) 2)))
}
\endlisp

\vspace{0.1in}
{\Large\bf 5. Substitution Model}

Use the substitution model to evaluate the following expression:
{\tt (average 4 (double 4))}

\vspace{-0.1in}
\beginlisp
\ANSWER{
$\Rightarrow$ ([procedure] 4 (double 4))
$\Rightarrow$             $\Rightarrow$ (double 4)
$\Rightarrow$             $\Rightarrow$ ([procedure] 4)
$\Rightarrow$             $\Rightarrow$ (* 2 4)
$\Rightarrow$ ([procedure] 4 8)
$\Rightarrow$ (/ (+ 4 8) 2)
$\Rightarrow$ ([procedure /] (+ 4 8) 2)
$\Rightarrow$             $\Rightarrow$ (+ 4 8)
$\Rightarrow$ ([procedure /] 12 2)
$\Rightarrow$ 6
}
\endlisp

\vspace{0.1in}
{\Large\bf 6. Subtleties}

Consider the following two definitions below.  How are they similar and how do
they differ?

\beginlisp
(define plus +)
\endlisp

\beginlisp
(define add 
  (lambda (x y) 
    (+ x y)))
\endlisp

\vspace{0.1in}
{\Large\bf 7. More Subtleties}

What do think should be the values of the following expressions?  
(Note: these are {\bf not} things you have to memorize.)

\begin{tabular}{p{2.5in}|p{4in}}
{
\beginlisp
$\Rightarrow$ (+ 4)
\ANSWER{\tt ;Value: 4}
\endlisp

\beginlisp
$\Rightarrow$ (- 3)
\ANSWER{\tt ;Value: -3}
\endlisp

\beginlisp
$\Rightarrow$ (/ 5)
\ANSWER{\tt ;Value: 0.2}
\endlisp

\beginlisp
$\Rightarrow$ (/ 60 5 2 3)
\ANSWER{\tt ;Value: 2}
\endlisp
} & {
\beginlisp
$\Rightarrow$ (+)
\ANSWER{\tt ;Value: 0}
\endlisp

\beginlisp
$\Rightarrow$ (*)
\ANSWER{\tt ;Value: 1}
\endlisp

\beginlisp
$\Rightarrow$ (-)
\ANSWER{\tt ;Error: The procedure \#[compiled-procedure] requires 
at least one argment}
\endlisp
}
\end{tabular}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
