Chapter 3
Lists

from
Introduction to Programming with Mathematica,
2nd Ed.

by
Richard J. Gaylord
Samuel N. Kamin
Paul R. Wellin

Copyright 1995 TELOS/Springer-Verlag

Initialization (evaluate to turn off spell messages)

Off[General::spell]

Off[General::spell1]

3.1  Introduction

List[2.4, dog, Sin, "read my lips", {5, 3}, Pi, {}]

3.2  Creating and Measuring Lists

3.2.1 List Construction

Range[-4, 7, 3]

Range[4, 8]

Range[4]

Range[1.5, 6.3, .75]

Table[3 k, {k, 1, 10, 2}]

Table[i, {i, 1.5, 6.3, .75}]

Table[3 i, {i, 2, 5}]

Table[i^2, {i, 4}]

Table[darwin, {4}]

Table[Random[], {3}]

Table[i + j, {j, 1, 4}, {i, 1, 3}]

Table[i + j, {i, 1, 3}, {j,1,4}]

Table[i + j, {i, 1, 4}, {j,1,3}] //TableForm

Table[i + j, {i, 1, 3}, {j, 1, i}]

Table[i + j, {i, 1, 3}, {j, 1, i}] //TableForm

3.2.2 Dimensions of Lists

Length[{a, b, c, d, e, f}]

Length[{{{1,2}, {3,4}, {5,6}}, {{a,b}, {c,d}, {e,f}}}]

Dimensions[{{{1,2}, {3,4}, {5,6}}, {{a,b}, {c,d}, {e,f}}}]

Solutions to Exercises

Ex.1

Table[j, {i, 0, 8, 2}, {j, 0, i, 2}]

Table[2j, {i, 0, 4}, {j, 0, i}]

Ex.2

Table[Random[Integer, {-1, 1}], {10}]

Ex.3

Table[2 Random[Integer] - 1, {10}]

Table[(-1)^Random[Integer], {10}]

{1, -1}[[Table[Random[Integer, {1, 2}], {10}]]]

Ex.4

Table[f[i], {i, 5}]

Table[f[i, j], {i, 3}, {j, 4}]

Ex.5

Dimensions[{{{1, a}, {4, d}}, {{2, b}, {3, c}}}]

3.3  Working with the Elements of a List

3.3.1 Positions in a List

Position[{5, 7, 5, 2, 1, 4}, 5]

Position[{{a, b, c}, {d, e, f}}, f]

3.3.2 Extracting Elements and Rearranging Lists

Part[{2, 3, 7, 8, 1, 4}, 3]

{2, 3, 7, 8, 1, 4}[[3]]

{{1, 2}, {a, b}, {3, 4}, {c, d}, {5, 6}, {e, f}}[[2, 1]]

{2, 3, 7, 8, 1, 4}[[{2, 1}]]

Take[{2, 3, 7, 8, 1, 4}, 2]

Take[{2, 3, 7, 8, 1, 4}, -2]

Take[{2, 3, 7, 8, 1, 4}, {2, 4}]

Take[{2, 3, 7, 8, 1, 4}, {-5, -3}]

Take[{2, 3, 7, 8, 1, 4}, {-5, 4}]

Drop[{2, 3, 7, 8, 1, 4}, 2]

Drop[{2, 3, 7, 8, 1, 4}, -2]

Drop[{2, 3, 7, 8, 1, 4}, {3, 5}]

Delete[{2, 3, 7, 8, 1, 4}, 2]

Delete[{2, 3, 7, 8, 1, 4}, {{2}, {5}}]

First[{2, 3, 7, 8, 1, 4}]

Last[{2, 3, 7, 8, 1, 4}]

Rest[{2, 3, 7, 8, 1, 4}]

Select[{2, 3, 7, 8, 1, 4}, EvenQ]

Reverse[{2, 7, e, 1, a, 5}]

Sort[{2, 7, e, 1, a, 5}]

Sort[{{2, c}, {7, 9}, {e, f, g}, {1, 4.5}, {x, y, z}}]

RotateLeft[{2, 7, e, 1, a, 5}, 2]

RotateRight[{2, 7, e, 1, a, 5}, 2]

Flatten[{{{3, 1}, {2, 4}}, {{5, 3}, {7, 4}}}]

Flatten[{{{3, 1}, {2, 4}}, {{5, 3}, {7, 4}}}, 1]

Partition[{2, 3, 7, 8, 1, 4}, 2]

Partition[{2, 3, 7, 8, 1, 4}, 1, 2]

Partition[{2, 3, 7, 8, 1, 4}, 2, 1]

Transpose[{{5, 2, 7, 3}, {4, 6, 8, 4}}]

Transpose[{{5, 2, 7, 3}, {4, 6, 8, 4}, {6, 5, 3, 1}}]

Append[{2, 3, 7, 8, 1, 4}, 5]

Prepend[{2, 3, 7, 8, 1, 4}, 5]

Insert[{2, 3, 7, 8, 1, 4}, 5, 3]

ReplacePart[{2, 3, 7, 8, 1, 4}, 5, 2]

Solutions to Exercises

Ex.1

Position[{{2, 1, 10}, {9, 5, 7}, {2, 10, 4}, 
          {10, 1, 9}, {6, 1, 6}}, 9]

Ex.2

Transpose[{{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}, {x5, y5}}]

Ex.3

Table[{{1,0}, {-1,0}, {0,1}, {0,-1}}[[Random[Integer, {1,4}]]], {10}]

Ex.4

We first drop the first element in the list, then create a nested list of every other element in the remaining list, and finally unnest the resulting list.

Rest[{a, b, c, d, e, f, g}]

Partition[%, 1, 2]

Flatten[%]

Ex.5

{a, b, c, d}[[{3, 2, 4, 1}]]

Ex.6

Transpose[{{3, 2, 4, 1}, {a, b, c, d}}]

Sort[%]

Transpose[%]

%[[2]]

3.4  Working with Several Lists

Join[{2, 5, 7, 3}, {d, a, e, j}]

Union[{4, 1, 2}, {5, 1, 2}]

Union[{4, 1, 2, 5, 1, 2}]

Complement[{2, 8, 7, 4, 8, 3}, {3, 5, 4}]

Intersection[{2, 8, 7, 4, 8, 3}, {3, 5, 4}]

Solutions to Exercises

Ex.1

Join[{z}, {x, y}]

Ex.2

Join[{1, 2, 3, 4}, {a, b, c, d}]

Rest[%]

Partition[%, 1, 2]

Flatten[%]

3.5  Higher-Order Functions

Map[f, {3, 5, 7, 2, 6}]

Map[Reverse, {{a, b}, {c, d}, {e, f}}]

Map[Sort, {{2, 6, 3, 5}, {7, 4, 1, 3}}]

MapThread[g, {{a, b, c}, {x, y, z}}]

MapThread[Power, {{2, 6, 3}, {5, 1, 2}}]

MapThread[List, {{5, 3, 2}, {6, 4, 9}, {4, 1, 4}}]

Outer[f, {a, b}, {2, 3, 4}]

Outer[List, {a, b}, {2, 3, 4}]

Log[{1.2, 0.4, 3.7}]

Map[Log, {1.2, 0.4, 3.7}]

{4, 6, 3} + {5, 1, 2}

MapThread[Plus, {{4, 6, 3}, {5, 1, 2}}]

Attributes[Log]

Apply[f, List[1, 4, 5, 3]]

Apply[Plus, {1, 4, 5, 3}]

Apply[f, {{1, 2, 3}, {5, 6, 7}}]

Apply[Plus, {{1, 2, 3}, {5, 6, 7}}]

Apply[f, {{1, 2, 3}, {5, 6, 7}}, 2]

Apply[Plus, {{1, 2, 3}, {5, 6, 7}}, 2]

Solutions to Exercises

Ex.1

{{1, 2, 3}, {4, 5, 6}} //TableForm

Transpose[{{1, 2, 3}, {4, 5, 6}}]

Map[Reverse, %] //TableForm

Ex.2

This can be done either in three steps, or by using the \mv{Inner} function.

Transpose[{{1, 2}, {3, 4}}]

% * {x, y}

Apply[Plus, %]

Inner[Times, {{1, 2}, {3, 4}}, {x, y}, Plus]

3.6  Applying Functions to Lists Repeatedly

Nest[g, a, 4]

NestList[g, a, 4]

NestList[Cos, 0.85, 10]

{0.85, Cos[0.85], Cos[Cos[0.85]], Cos[Cos[Cos[0.85]]]}

Fold[f, 0, {a, b, c, d}]

FoldList[f, 0, {a, b, c, d}]

FoldList[Plus, 0, {a, b, c, d}]

FoldList[Plus, 0, {3, 5, 2, 4}]

FoldList[Times, 1, {3, 5, 2, 4}]

Inner[f, {a, b, c}, {d, e, f}, g]

Inner[Times, {a, b, c}, {d, e, f}, Plus]

Inner[List, {a, b, c}, {d, e, f}, Plus]

Solutions to Exercises

Ex.1

Table[(-1)^Random[Integer], {10}]

FoldList[Plus, 0, %]

Ex.2

{{1,0}, {-1,0}, {0,1}, {0,-1}}[[Table[Random[Integer, {1,4}], {10}]]]

FoldList[Plus, {0, 0}, %]

3.7  Strings and Characters

"It's the economy, stupid"

InputForm["It's the economy, stupid"]

StringLength["It's the economy, stupid"]

StringReverse["abcde"]

StringTake["abcde", 3]

StringDrop["abcde", -1]

StringPosition["abcde", "bc"]

StringInsert["abcde", "t", 3]

StringReplace["abcde", "cd"->"uv"]

Characters["abcde"]

Take[%, {2, 3}]

StringJoin[%]

ToCharacterCode["darwin"]

% - 32

FromCharacterCode[%]

Solutions to Exercises

Ex.1

"this is a test string"

StringTake[%, 1]

ToCharacterCode[%]

% - 32

FromCharacterCode[%]

StringDrop[%%%%%,1]

StringJoin[%%, %]

Ex.2

"73"

ToCharacterCode[%][[1]]

10(% - 48)

ToCharacterCode[%%%][[2]]

% - 48

% + %%%

Ex.3

"73"

ToCharacterCode[%]

%[[1]]

8(% - 48)

ToCharacterCode[%%%%]

%[[2]]

% - 48

% + %%%%

Ex.4

"10495"

ToCharacterCode[%]

% - 48

Reverse[Table[10^j, {j, 0, 4}]]

Dot[%, %%]