# List Processing (Lec04)

## yukita@k.hosei.ac.jp

### •Append

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

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.

### •Prepend

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

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.

### •Insert

See how the Insert function works.

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

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.

### •Delete

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

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.

### •Select

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

Boolean functions can be composed.

We can do this in a more transparent way.

### •Table and Others ( List generation)

### •First and Rest

### •Iteration as Recursion

The following function squares each item in the list.

We could do the same thing as follows.

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.

Let us do it with explicit iteration.

The filterEven function filters through even numbers.

Let us do it with explicit iteration.

### •Recursion

The myReverse function reverses a given list.

Note that the builtin function Reverse does the same thing.

### •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)