# Functional Programming II (Lec06)

## yukita@k.hosei.ac.jp

### •Fold

Horner's method is given by

Alternating sum is given by

The **myUnion**

function acts the same as builtin function **Union**

except that it does no sorting prior to output.

### •Thread

We are going to calculate the average scores of Database, Operating System, and Computer Architecture. To do this we extract the numerical data part from **data**

.

Compare the following. The first and second usage returns the same result, which is not what we expected. The third and fourth ones come up with what we need.

Let us try the third way with f replaced by Plus, which produces the list of the average scores of the three subjects.

Let us do it all in one-liner.

We will see, in the next section, much elegant solution to the averaging.

### •MapThread

The average scores are obtained as follows.

### •Map(revisited)

We would like to append a new field to each record. The new field contains the average score of the three subjects of each person.

### •Exercises

#### •Problem 1 (Mandatory)

Using **Fold**

, define the **sumTo**

function that returns

when called as **sumTo[n]**

.

##### •Solution

#### •Problem 2 (Optional)

Write a one-liner program that outputs the list of the variances of three subjects on input of **data**

given in the Section of "Thread." Use **MapThread**

for readability.

##### •Solution

Converted by *Mathematica*
(May 20, 2003)