List Processing (Lec04)

yukita@k.hosei.ac.jp

•Append

a = {1, 2, 3, 4, 5}

{1, 2, 3, 4, 5}

Append[a, 6]

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

The Append function does not alter its arguments.So, it is a pure function.Check if variable "a" remains the same.

a

{1, 2, 3, 4, 5}

If you want a side effect in which variable "a" is subject to change, then try the following and check if you get the expected result.

AppendTo[a, 6]

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

a

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

•Prepend

a

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

Prepend[a, 0]

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

The Prepend function does not alter its arguments.So,it is a pure function.Check if variable "a"remains the same.

a

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

If you want a side effect in which variable "a" is subject to change, then try the following and check if you get the expected result.

PrependTo[a, 0]

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

a

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

•Insert

a

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

See how the Insert function works.

Insert[a, 100, 3]

{0, 1, 100, 2, 3, 4, 5, 6}

The Insert function  is a pure function. To see this try the following.

a

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

If you want a side effect in which variable "a" is subject to change, then try the following and check if you get the expected result.

a = Insert[a, 100, 3]

{0, 1, 100, 2, 3, 4, 5, 6}

a

{0, 1, 100, 2, 3, 4, 5, 6}

•Delete

a

{0, 1, 100, 2, 3, 4, 5, 6}

Delete[a, 3]

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

The Delete function  is a pure function. To see this try the following.

a

{0, 1, 100, 2, 3, 4, 5, 6}

If you want a side effect in which variable "a" is subject to change, then try the following and check if you get the expected result.

a = Delete[a, 3]

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

a

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

•Select

a

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

The Select function selects all the items that match the criterion in the second argument.

Select[a, EvenQ]

{0, 2, 4, 6}

Select[a, OddQ]

{1, 3, 5}

Boolean functions can be composed.

Select[a, Not[OddQ[#]] &]

{0, 2, 4, 6}

We can do this in a more transparent way.

Select[a, Composition[Not, OddQ]]

{0, 2, 4, 6}

Select[a, Not @ OddQ @ # &]

{0, 2, 4, 6}

•Table and Others ( List generation)

List[1, 2, 3, 4, 5, 6]

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

Range[1, 6]

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

Range[6]

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

Range[2, 6]

{2, 3, 4, 5, 6}

Range[1, 10, 2]

{1, 3, 5, 7, 9}

Range[x, x + 5]

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

Table[i, {i, 1, 6}]

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

Table[i^2, {i, 1, 6}]

{1, 4, 9, 16, 25, 36}

Array[f, 6]

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

Array[f, 3, 6]

{f[6], f[7], f[8]}

Array[f, 3, x]

{f[x], f[1 + x], f[2 + x]}

Array[f, 6, 1, Plus]

f[1] + f[2] + f[3] + f[4] + f[5] + f[6]

•First and Rest

a = Range[10]

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

First[a]

1

Rest[a]

{2, 3, 4, 5, 6, 7, 8, 9, 10}

First[Rest[a]]

2

Rest[Rest[a]]

{3, 4, 5, 6, 7, 8, 9, 10}

•Iteration as Recursion

a = Range[10]

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

The following function squares each item in the list.

mySquare[x_List] = If[x == {}, {}, Prepend[mySquare[Rest[x]], First[x] * First[x]]]

If[x == {}, {}, Prepend[mySquare[Rest[x]], First[x] First[x]]]

mySquare[a]

{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

We could do the same thing as follows.

mySquare2[x_List] := Map[# * # &, x]

Note that if we defined mySquare with the = symbol, we would not get the expected result. This is because the RHS would be immediately evaluated with #*#& having no effect on x.

a

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

mySquare2[a]

{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

Let us do it with explicit iteration.

mySquare9[x_List] := Table[x[[i]] * x[[i]], {i, Length[x]}]

mySquare9[Range[10]]

{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

The filterEven function filters through even numbers.

filterEven[x_List] :=  If[x == {}, {},            ... bsp;             filterEven[Rest[x]]]]

filterEven[Range[10]]

{2, 4, 6, 8, 10}

Let us do it with explicit iteration.

filterEven9[x_List] :=  Module[{result = {}},  Do[If[EvenQ[x[[i]]], AppendTo[result, x[[i]]],  ... bsp; (* do nothing *)],        {i, 1, Length[x]}] ;     result]

filterEven9[Range[10]]

{2, 4, 6, 8, 10}

•Recursion

The myReverse function reverses a given list.

myReverse[x_List] = If[x == {}, {}, Append[myReverse[Rest[x]], First[x]]]

If[x == {}, {}, Append[myReverse[Rest[x]], First[x]]]

myReverse[a]

{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

Note that the builtin function Reverse does the same thing.

Reverse[a]

{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

•Exercises

•Problem 1

Define the myReverse9 function that does the same task as myReverse with explicit iteration using Table.

•Solution

•Problem 2

Define the function filterMultiples that takes a list x of integers as its first argument and natural number k as its second argument, and filters multiples of k. Implement this function in three ways: (1) using Module and Do-loop, (2) using the Select construct, and (3) using First, Rest, and recursion.

•Solution

Converted by Mathematica  (May 20, 2003)