% -*- Mode: Tex -*-

%\input /homes/welg/6001/fall96/6001mac.tex
%\input /zu/6001-devel/6001mac.tex
\input 6001mac.tex
\input{psfig}

\def\fbox#1{%
  \vtop{\vbox{\hrule%
              \hbox{\vrule\kern3pt%
                    \vtop{\vbox{\kern3pt#1}\kern3pt}%
                    \kern3pt\vrule}}%
        \hrule}}

\def\emptybox#1#2{\fbox{\vbox to #2{\hbox to #1 {\null}}}}

\begin{document}

\psetheader{Fall Semester, 1996}{Quiz I -- Solutions}

These are representative examples of solutions to the quiz problems.
Note that other answers are possible in some cases.  

\vskip 20pt

{\bf Question 0 (2 points)}  

The correct answer to the second part is {\tt your name}


\vskip 10pt

{\bf Question 1 (14 points)}  

The correct answers are:

b, b, c, b, a, b, c, c, e, c, d

\vskip 10pt

{\bf Question 2 (5 points)}  

The correct implementation is:

\begin{lisp}
(define precinct-name cadr)
(define registered caddr)
(define votes cadddr)
(define candidate car)
(define tally cdr)
\end{lisp}


\vskip 10pt

{\bf Question 3 (10 points)}  

%\emptybox{6in}{3in}
\bigskip

\centerline{\psfig{file=precinct.ps}}

\vskip 10pt
\newpage
{\bf Question 4 (16 points)}  

Part A.

\begin{lisp}
(define (precinct-total precinct)
  (define (votes-summary votes)
    (if (empty-votes? votes) 
        0
        (+ (tally (first-entry votes))
           (votes-summary (rest-entry votes)))))
  (votes-summary (votes precinct)))
\end{lisp}

Part B.

\begin{lisp}
(define (precinct-total precinct)
  (define (votes-summary votes so-far)
    (if (empty-votes? votes) 
        so-far
        (votes-summary (rest-entry votes)
                       (+ (tally (first-entry votes)) so-far))))
  (votes-summary (votes precinct) 0))
\end{lisp}



\vskip 10pt

{\bf Question 5 (10 points)}  

\begin{lisp}
(define (precinct-total precinct)
  (accumulate +
              0
              (map tally
                   (votes precinct))))
\end{lisp}


\vskip 10pt

{\bf Question 6 (18 points)}  

\begin{lisp}
(define (insert elt votes)
  (cond ((empty-votes? votes)
         (add-entry elt empty-votes))
        ((< (tally elt) (tally (first-entry votes)))
         (add-entry (first-entry votes)
                    (insert elt (rest-entries votes))))
        (else (add-entry elt votes))))
\null
(define (insert-all old-votes)
  (define (helper todo done)
    (cond ((empty-votes? todo) 
           done)
          (else (helper (rest-entries todo)
                        (insert (first-entry todo) done)))))
  (helper old-votes empty-votes))
\end{lisp}

\vskip 10pt

\newpage
{\bf Question 7 (10 points)}  

\begin{lisp}
(define (all-symbols? tree)
  (cond ((null? tree) \#t)
        ((pair? tree)
         (and (all-symbols? (car tree))
              (all-symbols? (cdr tree))))
        (else (symbol? tree))))
\end{lisp}


\vskip 10pt

{\bf Question 8 (15 points)}

The correct answers are:

\begin{lisp}
24
\null
(2 4 6)
\null
(2 (4 (6) 8))
\null
(1 2 3)
\null
(1 2 3 4)
\end{lisp}

\end{document}

