Skip to content

Instantly share code, notes, and snippets.

@balu
Created February 2, 2021 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balu/dadb3258ae943a92a9cadfaa28cab262 to your computer and use it in GitHub Desktop.
Save balu/dadb3258ae943a92a9cadfaa28cab262 to your computer and use it in GitHub Desktop.

Instructions

Use the following heuristic to write programs that manipulate tuples.

  1. Unpack the input tuples to obtain their components.
  2. Process the components to obtain components of the result.
  3. Combine the components of the result to obtain the output tuples.

Questions

  1. 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).

  2. 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?

  3. A rational number is a number of the form p/q where p and q are integers and q is not zero. What type would you choose to represent rationsls?

  4. Write a function rational_multiply that takes two rationals as input and produces their product as output.

  5. 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment