levo

Fundamentals of Functional Programming

Writing functional programs

  • 3 things to know
  • Paper 2

Overview

Recall questions on map, filter and reduce, what each does to a list of n elements, and composing them to answer a question about a set of data.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

What does map do?

Evaluation

Give an example of filter applied to a list, showing the result.

Process

Which higher-order function selects the marks of at least 40 from a list of results?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a higher-order function?

    Model answer

    One that treats functions as data: it either accepts a function among its arguments, or hands a function back as its result, or does both. This is only possible in a language where functions are first-class objects, since the function being passed has to be a value like any other. The point of it is generality — an operation such as 'do the same thing to every element' can be written once, with the particular thing left as a parameter to be supplied by the caller, rather than rewritten for each case.

  2. 2

    Is a function that returns a function, but takes only ordinary values, higher-order?

    Model answer

    Yes. The definition is met by taking a function as an argument or by returning one as a result, or by doing both, so returning one is sufficient on its own. A two-argument function written in curried form, add: integer → integer → integer, is higher-order for exactly that reason: add 4 returns a function.

  3. 3

    What does map do?

    Model answer

    It takes a function and a list, applies that function to every element in turn, and hands back a list of the results. The output has exactly as many elements as the input and keeps them in the same order, and the original list is left untouched — a new one is produced. Mapping a doubling function over [1, 2, 3] gives [2, 4, 6]. It is higher-order because the operation to perform arrives as an argument, which is what lets one implementation serve every elementwise transformation.

  4. 4

    Give an example of map applied to a list, showing the result.

    Model answer

    Applying a doubling function to the list [1, 2, 3, 4] with map gives [2, 4, 6, 8]. In Haskell this is written map (*2) [1, 2, 3, 4], which evaluates to [2, 4, 6, 8]. Applying a function that returns the length of a string gives a list of a different element type: map length ["cat", "donkey", "ox"] evaluates to [3, 6, 2], showing that map may change the type of the elements but never the number of them.

  5. 5

    What does filter do?

    Model answer

    It takes a predicate — a function returning true or false — and a list, tests every element against it, and returns a new list holding just those elements for which the test passed, in their original order. Nothing is transformed: the elements that survive are unchanged, and the result is shorter than the input, or the same length if everything passes, or empty if nothing does. Filtering [1, 2, 3, 4, 5, 6] with a test for evenness gives [2, 4, 6].

  6. 6

    Give an example of filter applied to a list, showing the result.

    Model answer

    Filtering the list [4, 7, 10, 13, 16] with the condition 'is even' gives [4, 10, 16]. In Haskell this is written filter even [4, 7, 10, 13, 16], which evaluates to [4, 10, 16]. The elements kept are unchanged and remain in their original order; only the ones failing the condition are absent.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

See a huge jump in your memorisation

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It trains answer structure

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Functional Programming, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.