Functional Programming Paradigm

4.12.1 (10 questions). Public Notes: AQA's A Level Computer Science

Define a function in functional programming.

A rule that for each element is set A, assigns a output chosen from set B, but not necessarily using every member of B.

Define a function in functional programming with domains and sets.

A mapping of a domain to a co-domain, where both a subsets of an object of some data type.

State the types in function f: A → B, and describe B.

A is the argument typeB is the result typef is the function type (the operation performed on domain A)B does not need to contain all members of its co-domain.

Explain why in functional programming a function can be argument and the result of a functional call.

In functional programming languages, functions are first class objects.

State the requirements for a first class object.

  1. Appear in expressions.
  2. Be assigned to a variable.
  3. Be assigned as arguments.
  4. Be returned in function calls.

Define function application, giving an example.

The process of giving inputs to a particular function. Where a set of two numbers is the single argument taken and all functions only take a single argument.

  • e.g. add(3,4)

Define partial function application, giving an example.

Fixing one argument, to create a more restricted, specialised function, based on the fact that a function can only take one input.

  1. Add3Numbers x y z = x + y + z
  2. Add3To2Numbers = Add3Numbers 3
Add3To2Numbers must be called with 2 arguments to fulfil the number of inputs for Add3Numbers, hence
  • Add3To2Numbers 4 1 >>> 8

Define composition of functions.

Combining two functions to create a new function, so that each function may be called separately and in conjunction.

Explain the restriction for combining functions

A domain of one function must be same as the co-domain of the other function.

    e.g. where italics is the domain.

  1. f ::: realreal → real
  2. g ::: real → real
  3. f o g is not possible, because the domain of f is a pair of reals, while the co-domain of g is a single real.

    g o f is possible, because the domain of g is a single real, and the co-domain of f is a single real

If f(x) = 3x + 4, and g(x) = x*2, what is the result of g o f (2) and f o g (2)

  1. g o f (2)
    1. 3(2) + 4 = 10
    2. 10 * 2 = 20

    Answer: g o f = 20

  2. f o g (2)
    1. (2)*2 = 4
    2. 3(4) + 4 = 16

    Answer f o g = 16

💡 The right most function is applied first!