levo

Fundamentals of Functional Programming

Partial Function Application and Composition

  • 3 things to know
  • Paper 2
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A driver is .
softwarehardwarestorage

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

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

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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