levo

Fundamentals of Functional Programming

Partial Function Application and Composition

  • 3 things to know
  • Paper 2

Overview

Recall questions on partial function application and how its types are written and grouped, practical reasons to use it, and composing functions and working out g ○ f and f ○ g.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Evaluation

Write the type of the two-argument function add in the partial function application scheme, and explain what add 4 gives.

Key term

A three-argument function volume multiplies three integers. Give its partial function application type, and describe what each successive application produces.

Key term

Given f: A → B and g: B → C, why does g ○ f have domain A and co-domain C?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What does it mean to apply a function partially?

    Model answer

    Partial function application means applying a function to fewer arguments than it finally needs, so that instead of a final result it returns a new function which takes the remaining arguments. The arguments are supplied one after another rather than all at once, and each application returns something that can be applied again until the last argument arrives and an ordinary value is produced.

  2. 2

    Write the type of the two-argument function add in the partial function application scheme, and explain what add 4 gives.

    Model answer

    Under that scheme the type is add: integer → (integer → integer). Read it from the outside in: add accepts one integer and gives back something whose own type is integer → integer, which is to say a function. So supplying just 4 produces no number at all; it produces a new function that stands ready to add 4 to whatever integer is handed to it next. Applying that result is a second and separate step, which is why (add 4) 7 evaluates to 11 — the first application builds the adder, the second uses it.

  3. 3

    The brackets in add: integer → (integer → integer) may be dropped. Write the result and explain why it means the same thing.

    Model answer

    It becomes add: integer → integer → integer, and it means the same thing because the arrow groups to the right by convention. A chain of arrows is always read from the right-hand end, so integer → integer → integer must be understood as integer → (integer → integer); the reading (integer → integer) → integer is never what the notation means, and would describe something quite different, a function whose own argument is a function. Written without brackets, the type reads naturally as add consuming its arguments one at a time, each application swallowing a single integer, until at last an integer result emerges.

  4. 4

    Why is it important that the arrow groups from the right rather than the left?

    Model answer

    Because the two readings describe entirely different functions. Read from the right, integer → (integer → integer) is a function that takes an integer and gives back a function — this is add in the partial application scheme. Read from the left, (integer → integer) → integer would be a function whose argument is itself a function from integer to integer and whose result is a single integer, which is a higher-order function of a quite different kind. The convention exists so that partial application can be written without brackets, which is the far commoner case.

  5. 5

    Write the partial function application type of a one-argument function, and explain why nothing changes for it.

    Model answer

    A one-argument function such as double: integer → integer already has just one arrow, so there is only one argument to supply and applying it produces the result immediately. Partial application has nothing to do here, because there are no remaining arguments for an intermediate function to wait for. The scheme only becomes interesting from two arguments upwards.

  6. 6

    Give the partial function application type of a three-argument function that multiplies three integers, and describe what each successive application produces.

    Model answer

    volume: integer → integer → integer → integer, which with brackets restored is integer → (integer → (integer → integer)). Applying volume to one integer, as in volume 2, returns a function of type integer → integer → integer. Applying that to a second integer, as in volume 2 3, returns a function of type integer → integer. Applying that to a third, as in volume 2 3 4, finally returns the integer 24. Each application removes exactly one arrow from the left of the type.

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

The 3-step method, run for you

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 makes facts automatic

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 gets the exam shape right

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.