CS 4536/CS536: Scheme Practice Problems

Simple Functions and Conditionals

  1. The local supermarket needs a program that can compute the value of a bag of coins. Define the program sum-coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag.

  2. An old-style movie theater has a simple profit function. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the function total-profit. It consumes the number of attendees (of a show) and produces how much income the attendees produce.

  3. Develop the function tax, which consumes the gross pay and produces the amount of tax owed. For a gross pay of $240 or less, the tax is 0%; for over $240 and $480 or less, the tax rate is 15%; and for any pay over $480, the tax rate is 28%.

  4. Write the program discount, which takes the name of an organization that someone belongs to and produces the discount (a percentage) that the person should receive on a purchase. Members of AAA get %10, members of ACM or IEEE get %15, and members of UPE get %20. All other organizations get no discount.

    Use discount to write purchase, which takes the price of an item and the name of an organization and produces the amount owed after the discount is applied.

User-defined datatypes

  1. Define a type for a grade record, which contains a midterm exam grade (non-negative number), final exam grade (non-negative number), and course grade (symbol or false, using the latter if no grade has been assigned yet).

  2. Write a function assign-grade that takes a grade record and produces a grade record. The produced grade record has the same exam grades as the original, but has computed the course grade from the exam grades. The course grade is A for an average above 85, B for an average between 70 and 84, C for an average between 55 and 69, and NR otherwise.

  3. Define a type for a student record, which contains a student's name, class year, and a grade record (use whatever type you like for the class year).

  4. Write a function new-student which consumes a student name and class year and produces a student record with the given name and class year, both exam grades initialized to 0 and the course grade initialized to false.

  5. Define a type for a student, where a student can be either an undergraduate, graduate, or non-degree student. Each contains the number of courses they have taken. Undergraduates have an advisor (indicated by name) and graduates have a department and which degree they are pursuing (MS or PhD).

  6. Write a function finished? which consumes a student and produces a boolean indicating whether the student has enough courses to graduate. Undergraduates need 45 courses, graduates need 11 for an MS and 60 for a PhD. Non-degree students are always finished.

Lists

  1. Write a function eliminate-large that consumes a list of numbers and produces a lis of all numbers from the original list that are larger than 10.

  2. Write a function elim-by-pred that consumes a predicate (function that returns boolean) on numbers and a list of numbers and produces the list of all numbers for which the given function returns false.

  3. Write a function average that consumes a list of numbers and produces its average. Return an error message if the list is empty.

  4. Write a function suffixes that consumes a list L (containing anything) and produces a list of all the suffixes of L. For example, (suffixes '(a b c d)) should produce ((a b c d) (b c d) (c d) (d) ()).

Trees

  1. Develop a datatype for family trees, where a family tree is either unknown or a person, where a person has a name (string), eye color (symbol), mother (family tree) and father (family tree).

  2. Write a function count-persons that consumes a family tree and produces the total number of people in that tree.

  3. Write a function count-gens that consumes a family tree and produces the number of generations in that tree.

  4. Write a function eye-colors that consumes a family tree and produces a list of all eye colors that appear in the tree. The list may contain duplicates.

    Hint: use append which consumes any number of lists and concatenates them.