Skip to content

Commit

Permalink
Updated list - definition (EN)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinyu95 committed Feb 26, 2024
1 parent b24ce43 commit cf53b58
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions others/appendix/list/list-en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ \chapter{List}
\section{Introduction}
\label{introduction}

List and array are build blocks for other complex data structure. Both hold multiple elements as a container. Array is a range of consecutive cells indexed by a number (address). It is typically bounded with fixed size. While list increases on-demand. One can traverse a list one by one from head to tail. Particularly in functional settings, list plays critical role to control the computation and logic flow\footnote{In low level, lambda calculus plays the most critical role as one of the computation model equivalent to Turing machine\cite{mittype}, \cite{unplugged}.}. Readers already be familiar with map, filter, fold are safe to skip this chapter, and directly start from chapter 2.
List and array are build blocks for other complex data structure. They both hold multiple elements as containers. Array has a range of consecutive cells indexed by a number (address). It is typically bounded with fixed size when declared; while list increases on-demand. One traverses a list of elements one by one from head to tail. list plays critical role to control the computation and logic flow particularly in functional settings\footnote{Modeled by lambda calculus at low level, which is equivalent to Turing machine\cite{mittype}, \cite{unplugged}.}. Readers who have already been familiar with map, filter, fold, and etc., are safe to skip this chapter, and directly start from chapter 2.

\section{Definition}
\index{List!definition}

List, or singly linked-list is a data structure recursively defined as: A {\em list} is either empty, denoted as $[\ ]$ or NIL; or contains an element (also called key) and liked with a sub-{\em list} (called next). \Cref{fig:list-example} shows a list of nodes. Every node links to the next or NIL (the last). We often define list with the compound structure\footnote{In most cases, the data stored in list have the same type. However, there is also heterogeneous list, like the list in Lisp for example.}, for example:
List, or singly linked-list, is a data structure recursively defined as: A {\em list} is either empty, denoted by $[\ ]$ or NIL; or contains an element (also called key) and is linked with a sub-{\em list} (called next). \Cref{fig:list-example} shows a list of nodes; every node links to the next or NIL (the last). We often define list with some compound structure\footnote{We assume the data stored in list have the same type. However, there is also heterogeneous list, e.g., the list in Lisp.}, for example:


\begin{figure}[htbp]
Expand All @@ -65,11 +65,11 @@ \section{Definition}
\end{lstlisting}

\index{List!empty} \index{List!empty testing}
Many programming environments support the NIL concept. There are two ways to represent the empty list: use NIL (or null, or $\nil$) directly, or create a list, but put nothing as $[\ ]$. From implementation perspective, NIL need not allocate any memories, while $[\ ]$ does.
Many programming environments support the NIL concept. There are two ways to represent the empty list: use NIL (or null, or $\nil$) directly, or create a list, but put nothing as $[\ ]$. From implementation perspective, NIL doesn't need any allocated memories, while $[\ ]$ does.

\subsection{Access}
\index{List!head} \index{List!tail} \index{List!Construction} \index{List!cons}
Given a none empty list $X$, define two functions\footnote{We often write function $f(x)$ as $f\ x$, and $f(x, y, ..., z)$ as $f\ x\ y\ ...\ z$.} to access the first element, and the rest sub-list. They are often called as \textit{first}\ $X$ and \textit{rest}\ $X$, or $head\ X$ and $tail\ X$\footnote{They are named as \texttt{car} and \texttt{cdr} in Lisp due to the design of machine registers\cite{SICP}.}. Conversely, we can construct a list from an element $x$ and another list $xs$ (can be empty), as $x \cons xs$. It is called the \texttt{cons} operation. We have the following equations:
Given a none empty list $X$, define two functions\footnote{We often write function $f(x)$ as $f\ x$, and $f(x, y, ..., z)$ as $f\ x\ y\ ...\ z$.} to access the first element and the rest sub-list. They are often named as \textit{first}\ $X$ and \textit{rest}\ $X$, or $head\ X$ and $tail\ X$\footnote{They are named as \texttt{car} and \texttt{cdr} in Lisp due to the design of machine registers\cite{SICP}.}. Conversely, we construct a list from an element $x$ and another list $xs$ (can be empty), as $x \cons xs$. It is called the \texttt{cons} operation.

\be
\begin{cases}
Expand All @@ -79,16 +79,17 @@ \subsection{Access}
\label{eq:list-head-tail}
\ee

For a none empty list $X$, we also denote the first element as $x_1$, and the rest sub-list as $X'$. For example, when $X = [x_1, x_2, x_3, ...]$, then $X' = [x_2, x_3, ...]$.
For a none empty list $X$, we often denote the first element as $x_1$, and the rest sub-list as $X'$. For example, when $X = [x_1, x_2, x_3, \dotsc]$, then $X' = [x_2, x_3, \dotsc]$.

\begin{Exercise}[label={ex:list-eq}]
\Question{For list of type $A$, suppose we can test if any two elements $x, y \in A$ are equal, define the algorithm to test if two lists are equal.}
\Question{For list of type $A$, suppose we can test if any two elements $x, y \in A$ are equal; define the algorithm to test if two lists are equal.}
\end{Exercise}

\begin{Answer}[ref={ex:list-eq}]
\Question{For list of type $A$, suppose we can test if any two elements $x, y \in A$ are equal, define the algorithm to test if two lists are equal.
\Question{For list of type $A$, suppose we can test if any two elements $x, y \in A$ are equal; define the algorithm to test if two lists are equal.

We use type constraint to ensure compare lists of the same type.
\vspace{3mm}
We use type constraint to assert comparing lists of the same type.
\begin{Haskell}
(==) :: [a] -> [a] -> Bool
[] == [] = True
Expand Down

0 comments on commit cf53b58

Please sign in to comment.