Functional programming · A level · AQA 7517 4.12.1.1 · about 20 min
Functions as mappings, function type f: A to B, domain and co-domain, and function application with arguments from a Cartesian product.
[1 mark]A function has type f: A → B. What is B called?
[1 mark]is_even takes a whole number and returns True or False. What is its function type?
[1 mark]Which statements about a function f: A → B are true?
Tick every answer that is true.
[1 mark]add has type integer × integer → integer. What is integer × integer called?
[1 mark]In function application, how many arguments does add(3, 4) strictly take, if add: integer × integer → integer?
[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))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.
quadrant(x, y) that returns 1, 2, 3 or 4 for a point on the mat.compass8(degrees: int) -> str with the eight points N, NE, E, SE, S, SW, W, NW. What changes, and what stays the same?battery() returns a whole percentage. Write down a sensible function type for it. Why is it not really a function in the mathematical sense?