% -*- Mode: Tex -*-

%\input /home/welg/6001mac.tex
\input /homes/welg/6001/6001mac.tex
%\input /zu/scheme/6001ARCHIVE/fall89/6001mac.tex

\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}}}}

\def\pscenter#1{\hbox to\hsize{\hss\psadobenomag{#1}\hss}}

\begin{document}

\psetheader{Spring Semester, 1998}{Recitation -- Friday, February 13}

{\Large\bf 0. Announcements}

$\cdot$ Problem Set 1 is due {\bf now}.  Late work
is not accepted.  Solutions will be posted on the web. 

$\cdot$ No classes Monday.  Tuesday follows a Monday schedule.
People in Tuesday tutorials should try to go to another tutorial.

\vskip .25in
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\Large\bf 1. Student Presentation}

Here are some procedures that double or triple their argument: 

\beginlisp
(define double (lambda (x) (* 2 x)))
(define triple (lambda (x) (* 3 x)))
\endlisp

Write a procedure that takes a number as argument, and returns a
procedure that multiplies its argument by the number, for example:

\beginlisp
(define doubler (make-mul 2))
(doubler 5)
;Value: 10
\endlisp

\vskip .25in

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

{\Large\bf 2. Higher Order Procedures}

Write a function {\tt swap} that takes a function f of two variables, and
returns a funtion of two variables that is f with the variables
swapped:   {\tt (f x y) == ((swap f) y x)} For example,  \\
{\tt ((swap -) 4 5) $\Rightarrow$ 1}. 

\beginlisp
(define swap
  (lambda (f)
    (lambda (x y)
      (f y x))))
\endlisp

\vskip .25in

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

{\Large\bf 3. More Higher Order Procedures}

Write a function {\tt fmax} that takes two functions f and g (that
each take a number) and returns a function, that when called with a
number $x$, returns the max of $f(x)$ and $g(x)$.  For example,

\beginlisp
(define max-sqr-sqrt (fmax sqr sqrt))
(max-sqr-sqrt 3) == 9
(max-sqr-sqrt 0.25) == 0.5
\endlisp
\vskip .05in
\beginlisp
(define fmax 
  (lambda (f g)
    (lambda (x)
      (let ((fx (f x))
            (gx (g x)))
        (if (> fx gx) fx gx)))))
\endlisp

% \vskip .25in
\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\Large\bf 4. Compose and Repeated}

\beginlisp
(define compose 
  (lambda (f g)
    (lambda (x) (f (g x)))))
\null
(define repeated 
  (lambda (proc n)
    (if (= n 0)
        identity
        (compose proc (repeated proc (- n 1))))))
\endlisp

To see how this is working, use the substitution model to evaluate the
folloing expression:

\beginlisp
((repeated square 2) 3)
\endlisp

\vskip 1.7in
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\Large\bf 5. Let and Lambda}

\beginlisp
(let ((<var1> <exp1>)
      (<var2> <exp2>)
      ...
      (<varn> <expn>))
  <body>)
\endlisp

The let statement is equivalent to the following 

\beginlisp
((lambda (<var1> <var2> ... <varn>)
     <body>)
 <exp1>
 <exp2>
 ... 
 <expn>)
\endlisp

Compare the following two examples.  First use let:

\beginlisp
(let ((a 2) 
      (b 5))
  (+ a b))
\endlisp

And now use lambda.

\beginlisp
((lambda (a b) (+ a b)) 2 5)
\endlisp

Question: Why won't this work?  (Hint: Unravel the syntactic sugar!)

\beginlisp
(let ((x 5)
      (y (* x x)))
  (+ x y))
\endlisp

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
