Use the following heuristic to write programs that manipulate tuples.
- Unpack the input tuples to obtain their components.
- Process the components to obtain components of the result.
- Combine the components of the result to obtain the output tuples.
-
Write a function
vadd
that takes two pairs of numbers and performs component-wise addition. i.e., it implements vector addition(a, b) + (c, d) = (a+c, b+d)
. -
Write a function that takes two people as input and returns the name of the older person. What type would you choose to represent people?
-
A rational number is a number of the form
p/q
wherep
andq
are integers and q is not zero. What type would you choose to represent rationsls? -
Write a function
rational_multiply
that takes two rationals as input and produces their product as output. -
I assume you are all familiar with rock, paper, scissors. Write a function
winner
that takes two hands in the game as input and outputs the result. What types would you choose for inputs and outputs?