\include{6001mac}
\begin{document}
\psetheader{IAP 2005}{Lecture 4}

\section*{Scheme}

\begin{enumerate}
\item {\bf Special Forms}
  \begin{enumerate}
  \item {\large {\it define} (sugared form)} - {\tt (define ({\it
    name parameters}) {\it expressions})}\\ This form is equivalent to
    {\tt (define {\it name} (lambda ({\it parameters}) {\it
    expressions}))}.
    \vspace{.5in}
  \item {\large {\it let}} - {\tt (let {\it bindings body})}\\ Binds the
    given bindings for the duration of the body.  The bindings is a
    list of {\tt ({\it name value})} pairs.  The body consists of one
    or more expressions which are evaluated in order and the value of
    last is returned.
    \vspace{1in}
  \end{enumerate}
\end{enumerate}

\section*{Problems}

\begin{enumerate}
\item Guess the value, then evaluate the expression in scheme.  If
your guess differs from the actual output, try desugaring any relevant
expressions.

\begin{verbatim}
(define (foo x)
  (+ x 3))

foo

(foo 5)

(define bar 5)

(define (baz) 5)

bar

baz

(bar)

(baz)

(let ((a 3)
      (b 5))
  (+ a b))

(let ((+ *)
      (* +))
  (+ 3 (* 4 5)))

(define m 3)
(let ((m (+ m 1)))
  (+ m 1))

(define n 4)
(let ((n 12)
      (o (+ n 2)))
  (* n o))

\end{verbatim}

\end{enumerate}

\section*{Data Structures}

\subsection*{New procedures}
\begin{enumerate}
\item {\tt (cons a b)} - Makes a cons-cell (pair) from a and b
\item {\tt (car c)} - extracts the value of the first part of the pair
\item {\tt (cdr c)} - extracts the value of the second part of the pair
\item $\mathtt{(c}\frac{a}{d}\frac{a}{d}\frac{a}{d}\frac{a}{d}\mathtt{r\ \ c)}$ - shortcuts
\item {\tt (list a b c ...)} - builds a list of the arguments to the procedure
\item {\tt (list-ref lst n)} - returns the {\it n}th element of lst
\item {\tt (append l1 l2)} - makes a new list containing the elements of both lists
\item {\tt (null? lst)} - is {\tt lst} the empty list?
\end{enumerate}

\section*{Problems}

\begin{enumerate}
\setcounter{enumi}{1}
\item Draw box-and-pointer for the values of the following expressions.
\begin{verbatim}
(list 1 2 3)

(cons 3 (list 1 2))

(cons 1 (cons 3 (cons 5 nil)))

(list (list 3))


\end{verbatim}

\item Write expressions whose values will print out like the
following.
\begin{verbatim}
(1 2 3)

((1 2) (3 4) (5 6))

((4 7) 2)


\end{verbatim}

\item Write expressions using {\tt car} and {\tt cdr} that will return
4 when the {\tt lst} is bound to the following values:
\begin{verbatim}
(7 6 5 4 3 2 1)


((7) (6 5 4) (3 2) 1)


(7 (6 (5 (4 (3 (2 (1)))))))


(7 ((6 5 ((4)) 3) 2) 1)


\end{verbatim}

\end{enumerate}

\end{document}
