The answersDownload the PDF
Worksheet

A13.2 Function types and function application

Functional programming · A level · AQA 7517 4.12.1.1 · about 20 min

BugBotLab
NameClassDate

What this lesson is about

Functions as mappings, function type f: A to B, domain and co-domain, and function application with arguments from a Cartesian product.

Questions 6 marks in all

  1. [1 mark]A function has type f: A → B. What is B called?

    1. AThe co-domain
    2. BThe domain
    3. CThe argument type
    4. DThe Cartesian product
  2. [1 mark]is_even takes a whole number and returns True or False. What is its function type?

    1. Ais_even: integer → Boolean
    2. Bis_even: Boolean → integer
    3. Cis_even: integer × Boolean → integer
    4. Dis_even: integer → integer
  3. [1 mark]Which statements about a function f: A → B are true?

    Tick every answer that is true.

    1. AA and B are subsets of objects in some data type
    2. BEach value in A is mapped to exactly one value in B
    3. CEvery value in B must be the result for some value in A
    4. DA is the set of results the function gives
  4. [1 mark]add has type integer × integer → integer. What is integer × integer called?

  5. [1 mark]In function application, how many arguments does add(3, 4) strictly take, if add: integer × integer → integer?

    1. AOne, the pair (3, 4)
    2. BTwo, 3 and 4
    3. CNone, it is a constant
    4. DThree, 3, 4 and the result
  6. [1 mark]What does this program print?

    def compass(degrees):
        return "NESW"[((degrees + 45) % 360) // 90]
    
    print(compass(44), compass(45), compass(224), compass(315))

The task: the compass type

Write compass with its type hints, exactly as def compass(degrees: int) -> str:. Its domain is the whole numbers 0 to 359, and it returns "N" for 0 to 44 and 315 to 359, "E" for 45 to 134, "S" for 135 to 224 and "W" for 225 to 314. Then: 1. Apply compass to every value in its domain, collect the different results, and print them sorted alphabetically and separated by spaces, as range: <letters>. Work the letters out; do not type them into the print. 2. Turn the robot right by 90 degrees four times. After each turn, print facing <letter>, where the letter is compass applied to the robot's heading() rounded to a whole number and taken MOD 360.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def compass(degrees):
    return "N"

print("range:", compass(0))

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/a13-2-function-types-and-application/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Give the type, domain and co-domain of a function quadrant(x, y) that returns 1, 2, 3 or 4 for a point on the mat.
  2. Write compass8(degrees: int) -> str with the eight points N, NE, E, SE, S, SW, W, NW. What changes, and what stays the same?
  3. battery() returns a whole percentage. Write down a sensible function type for it. Why is it not really a function in the mathematical sense?