CS 250 - Programming Languages
Spring 2001

Assignment 5 -- Due April 30 at 10:30am.

You must turn in the answers to these questions both in hardcopy and by e-mail to echown@bowdoin.edu.

  1. Write either a quicksort or a mergesort function (groups larger than 1 must choose quicksort). For reference purposes, there is a bubble sort implemented in your book. Do not use any system predicates to do primary tasks for you (e.g. do not use the existing merge function, write your own called merger). For quicksort, write your partition function with the form partition(list_to_partition, partition_element, littles, bigs).
  2. Write a program for the relation better-poker-hand(Hand1,Hand2,Hand) that succeeds if Hand is the better poker hand between Hand1 and Hand2 (for your purposes, just imagine that the function computes Hand). For those unfamiliar with poker, here are some rules necessary for answering this excercise.
    1. The order of cards is 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
    2. Each hand consists of five cards.
    3. The rank of the hands in ascending order is no pairs < one pair < two pairs < three of a kind < flush < straight < full house < four of a kind < straight flush.
    4. Where two cards have the same rank, the higher denomination wins, for example, a pair of kings beats a pair of 7's.

    Hints: (1) Represent a poker hand by a list of terms of the form card(Suit, Value). For example, a hand consisting of the Ace of Spades and the nine of Clubs would be represented by the list [card(spades,ace),card(clubs,9)]. (2) It may be helpful to define relations such as has_flush(Hand), which is true if all of the cards in Hand are of the same suit. (3). The number of cases to consider is reduced if the hand is first sorted.

  3. Write functions to do polynomial addition and subtraction in Lisp. You will probably want to represent polynomials as lists of numbers. E.g. (5 6) would be 5 + 6X. Your polynomial addition function should have no more recursive calls than the shorter of the two polynomials. (e.g. (poly-add '(1 3 2) '(1 5 8 2 4)) should only make three recursive calls).