• Home
  • Textbooks
  • Computer science with Mathematica: theory and practice for science, mathematics, and engineering
  • List Processing and Recursion

Computer science with Mathematica: theory and practice for science, mathematics, and engineering

Roman Maeder

Chapter 9

List Processing and Recursion - all with Video Answers

Educators


Chapter Questions

Problem 1

Here is an abstract data type for lists:
$\square$ Constants: { } , the empty list
$\square$ Selectors:
First [list], the first element of the list
Rest [list], the rest of the list without its first element
$\square$ Constructors:
Prepend[list, elem], gives a new list whose first element is elem and whose rest is list
$\square$ Predicates:
ListQ [list], returns True, if list is a list
Define the following functions on lists, using rules (no loops or branches, but conditional rules if necessary). Lists may be accessed only with the given selectors and predicates.
1. join $\left[l_1, l_2\right]$ : this function joins two lists
2. flatten[list ]: this function turns a nested list into a linear list
3. count $[l, e]$ : counts how often the element $c$ occurs in the linear list $l$ (Use $===$ for testing equality)
Examples:
These two lists are simply spliced together.
Make sure to treat special cases correctly.
All inner parentheses are removed.
An empty list is removed completely
The element a occurs twice.
$$
\begin{aligned}
& \operatorname{In}[1]:=\operatorname{join}[\{a, b, c\},\{x, y\}] \\
& \text { Out }[1]=\{a, b, c, x, y\} \\
& \operatorname{In}[2]:=j o i n[\{\},\{x,\{y, z\}\}] \\
& \text { Out }[2]=\{x,\{y, z\}\} \\
& \operatorname{In}[3]:=\{1 \operatorname{atten}[\{\{a\},\{b, c\}, d\}] \\
& \text { Out }[3]=\{a, b, c, d\} \\
& \operatorname{In}[4]:=\{1 \text { atten }[\{\{\},\{\{1\}, 2\}\}] \\
& \text { Out }[4]=\{1,2\} \\
& \operatorname{In}[5]:=\operatorname{count}[\{a, b, a, x\}, a] \\
& \text { Out }[5]=2
\end{aligned}
$$
In this list it occurs only once because the $\operatorname{In}[6]:=\operatorname{count}[\{(a\}, a, b\}, a]$ list $\{a\}$ is not the same as the symbol a. Out $[6]=1$

Check back soon!
04:02

Problem 2

Consider these definitions. What does the function $\mathrm{f}[]$ do?
$a\left[\{\}, e_{-}\right]:=\{\}$
a[1_List, e_] := Prepend[ a[Rest[1], e], Prepend[First[1], e] ]
$f[\{\}]:=\{\{\}\}$
$f[1$ List $]:=\operatorname{Join}[a[f[\operatorname{Rest}[1]], \operatorname{First}[1]], f[\operatorname{Rest}[1]]]$
1. What is $f[\{x\}], f[\{x, y\}]$ and $f[\{x, y, z\}]$ ?
2. If $\mathrm{f}$ is called with a list of $n \geq 0$ elements (i.e., in the form $\mathrm{f}\left[\left\{e_1, e_2, \ldots, e_n\right\}\right]$ ), what length does the result have?
3. Which well-known function is implemented by $\mathrm{f}\left[\left\{e_1, e_2, \ldots, e_n\right\}\right]$ ? Assume that all $e_i$ are pairwise distinct.

JW
Julie Wyman
Numerade Educator

Problem 3

Consider the function Mystery [list] (list is a LISP list, see Section 9.1.2):
$$
\begin{aligned}
& \text { Mystery }\left[1_{-}\right]:=\operatorname{mystery}[1, \text { nil }] \\
& \text { mystery }\left[n i 1, r_{-}\right]:=r \\
& \text { mystery }\left[1_{-}, r_{-}\right]:=\operatorname{mystery}\left[\operatorname{cdr}[1], \operatorname{cons}\left[\operatorname{car}[1], r_1\right]\right]
\end{aligned}
$$
1. Describe what the function Mystery [list $]$ does.
2. How many calls of mystery[] occur in the computation of Mystery[l], if $l$ is a list of length $n$ ?
3. Implement Mystery[] directly with a While loop (i.e., without the auxiliary function mystery []). Use Module to declare local variables.

Check back soon!

Problem 4

Choose one of the sorting methods from Section 6.2 that can be used for sorting LISP lists (Section 9.2) and implement it. The asymmetric access to lists (access is possible only to the first element and to the rest of a list) makes it necessary to adapt the algorithms for efficiency.

Check back soon!