
\begin{code}
(defun kons (kar-part kdr-part)
  \#'(lambda (operation value)
      (case operation
        ((get-first) kar-part)
        ((get-rest) kdr-part)
        ((set-first) (setf kar-part value))
        ((set-rest) (setf kdr-part value))
        (t (error "Bad operation for KONS object")))))
\end{code}

\noindent When KONS is applied, it returns a closure.

\begin{code}
        +--> (setf k-object (kons 2 (kons 3 nil)))
        |  +--> (kons 2 (kons 3 nil))
        |  |  2 $\longrightarrow$ 2
        |  |
        |  |  +--> (kons 3 nil)
        |  |  |  3 $\longrightarrow$ 3
        |  |  |  NIL $\longrightarrow$ NIL
        |  |  \#\#\#> Apply KONS to 3 and NIL
        |  |  \#     create local variable KAR-PART with value 3
        |  |  \#     create local variable KDR-PART with value NIL
        |  |  \#  +--> \#'(lambda (operation) ...)
        |  |  \#  |
        |  |  \# |A|
        |  |  \#  |
        |  |  \#  +--> \#<Closure A>
        |  |  \#\#\#> Result of KONS is \#<Closure A>
        |  |
        |  \#\#\#> Apply KONS to 2 and\ \#<Closure A>
        |  \#     create local variable KAR-PART with value 2
        |  \#     create local variable KDR-PART with value \#<Closure A>
        |  \#  +--> \#'(lambda (operation) ...)
        |  \#  |
        |  \# |B|
        |  \#  |
        |  \#  +--> \#<Closure B>
        |  \#\#\#> Result of KONS is \#<Closure B>
        |   set global variable K-OBJECT to \#<Closure B>
        +--> \#<Closure B>
\end{code}

\noindent The inner application of the KONS function results in the closure
A, and the outer application of KONS returns the closure B\@.  Both closures
have their own variables named KAR-PART and KDR-PART, which were created by
KONS\@.  The complication here is that since these closures are returned as
function results, they exist outside of the boundaries of the contours in
which they were created.  So when we want to find or change the value of a
variable, say, KAR-PART in closure A, we will have to look back in the
evaltrace diagram for the point where closure was created, and then use that
environment.  In order to make finding this point easier, the arrow for the
closure creation is labeled.  Let's continue with this example.

\begin{code}
(defun kar (klist)
  (funcall klist 'get-first nil))
 
(defun kdr (klist)
  (funcall klist 'get-rest nil))
\end{code}

\noindent The purpose of the functions KAR and KDR is to extract the values of
the variables KAR-PART and KDR-PART, respectively, from the closure argument,
KLIST.

\begin{code}
        +--> (kar (kdr k-object))
        |  +--> (kdr k-object)
        |  |  K-OBJECT $\longrightarrow$ \#<Closure B>
        |  \#\#\#> Apply KDR to \#<Closure B>
        |  \#     create local variable KLIST with value \#<Closure B>
        |  \#  +--> (funcall klist 'get-rest nil)
        |  \#  |
        |  \#  \#\#\#> Apply FUNCALL to \#<Closure B>, GET-REST, and NIL
        |  \#  \#  %%%> Apply \#<Closure B> to GET-REST and NIL
        |  \#  \#  %     create local variable OPERATION with value GET-REST
   |    |  \#  \#  %     create local variable VALUE with value NIL
  |B|<-----------%  +--> (case (operation) ...)
   |    |  \#  \#  %  |  kdr-part $\longrightarrow$ \#<Closure A>
        |  \#  \#  %  +--> \#<Closure A>
        |  \#  \#  %%%> \#<Closure A>
        |  \#  \#\#\#> Result of FUNCALL is \#<Closure A>
        |  \#\#\#> Result of KDR is \#<Closure A>
        |
        \#\#\#> Apply KAR to \#<Closure A>
        \#     create local variable KLIST with value \#<Closure A>
        \#  +--> (funcall klist 'get-first)
        \#  |
        \#  \#\#\#> Apply FUNCALL to \#<Closure A>, GET-FIRST, and NIL
        \#  \#  %%%> Apply \#<Closure A> to GET-FIRST and NIL
        \#  \#  %     create local variable OPERATION with value GET-FIRST
   |    \#  \#  %     create local variable VALUE with value NIL
  |A|<--------%  +--> (case (operation) ...)
   |    \#  \#  %  |  KAR-PART $\longrightarrow$ 3
        \#  \#  %  +--> 3
        \#  \#  %%%> 3
        \#  \#\#\#> Result of FUNCALL is 3
        \#\#\#> Result of KAR is 3
\end{code}

\noindent The arcs for the closure's parent contours lead to labels that
direct us to look back for the contour with the same label.

If the value of a variable in a closure is assigned to, then this value is
kept with the label so that we will see this new value first.  This is shown
in the following:

\begin{code}
(defun set-kar (klist value)
  (funcall klist 'set-first value))
 
(defun set-kdr (klist value)
  (funcall klist 'set-rest value))
\end{code}

\small
\begin{code}
                  +--> (set-kar k-object 5)
                  |  K-OBJECT $\longrightarrow$ \#<Closure B>
                  |  5 $\longrightarrow$ 5
                  \#\#\#> Apply SET-KAR to \#<Closure B> and 5
                  \#     create local variable KLIST with value \#<Closure B>
                  \#     create local variable VALUE with value 5
                  \#  +--> (funcall klist 'set-first value)
                  \#  |
                  \#  \#\#\#> Apply FUNCALL to \#<Closure B>, SET-FIRST, and 5
                  \#  \#  %%%> Apply \#<Closure B> to SET-FIRST and 5
                  \#  \#  %     create local variable OPERATION with value GET-FIRST
                  \#  \#  %     create local variable VALUE with value 5
         |        \#  \#  %  +--> (case (operation) ...)
        |B|<------------%  |  +--> (setf kar-part value)
         |        \#  \#  %  |  |  VALUE $\longrightarrow$ 5
 |KAR-PART --> 5| \#  \#  %  |  |     set local variable KAR-PART to value 5
         |        \#  \#  %  |  +--> 5
                  \#  \#  %  +--> 5
                  \#  \#  %%%> 5
                  \#  \#\#\#> Result of FUNCALL is 5
                  \#\#\#> Result of SET-KAR is 5
\end{code}
\normalsize

\noindent The variable named KAR-PART in the contour for closure B is assigned
a new value.  This assignment is noted underneath the contour label.  So now
later when we have to find the value of the variable named KAR-PART in closure
B's environment, we will find, as we look back through the evaltrace diagram
for the correctly labelled contour, that it has been assigned the value 5.
