Parcourir la source

added description of R_3 syntax and an example

Jeremy Siek il y a 9 ans
Parent
commit
63614cb010
1 fichiers modifiés avec 41 ajouts et 8 suppressions
  1. 41 8
      book.tex

+ 41 - 8
book.tex

@@ -2933,17 +2933,50 @@ if_end1327:
 \end{figure}
 
 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\chapter{Tuples and Garbage Collection}
+\label{ch:tuples}
 
+In this chapter we study the compilation of mutable tuples (called
+vectors in Racket). Figure~\ref{fig:r3-syntax} defines the syntax for
+$R_3$, which includes three new forms for creating a tuple, reading an
+element of a tuple, and writing an element into a tuple. The following
+program shows the usage of tuples in Racket. We create a 3-tuple
+\code{t} and a 1-tuple. The 1-tuple is stored at index $2$ of the
+3-tuple, showing that tuples are first-class values.  The element at
+index $1$ of \code{t} is \code{\#t}, so the ``then'' branch is taken.
+The element at index $0$ of \code{t} is $40$, to which we add the $2$,
+the element at index $0$ of the 1-tuple.
+\begin{lstlisting}
+(program
+  (let ([t (vector 40 #t (vector 2))])
+    (if (vector-ref t 1)
+        (+ (vector-ref t 0)
+           (vector-ref (vector-ref t 2) 0))
+        44)))
+\end{lstlisting}
 
+\begin{figure}[tbp]
+\centering
+\fbox{
+\begin{minipage}{0.96\textwidth}
+\[
+\begin{array}{lcl}
+  \Exp &::=& \ldots \mid (\key{vector}\;\Exp^{+}) \mid 
+    (\key{vector-ref}\;\Exp\;\Exp) \\
+  &\mid& (\key{vector-set!}\;\Exp\;\Exp\;\Exp)\\
+  R_3 &::=& (\key{program} \; \Exp)
+\end{array}
+\]
+\end{minipage}
+}
+\caption{The $R_3$ language, an extension of $R_2$
+  (Figure~\ref{fig:r2-syntax}).}
+\label{fig:r3-syntax}
+\end{figure}
 
 
 
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\chapter{Tuples and Heap Allocation}
-\label{ch:tuples}
-
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \chapter{Garbage Collection}
 \label{ch:gc}
@@ -2960,8 +2993,8 @@ if_end1327:
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\chapter{Mutable Data}
-\label{ch:mutable-data}
+%\chapter{Mutable Data}
+%\label{ch:mutable-data}
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \chapter{Dynamic Typing}