12. Structured Patterns:  Homework

Last revision:  February 18 1998

Author: YourNameGoesHere

Poker Hands

You are to define and show working examples of a function "poker[]".  The function "poker[]" should take five integers, each between 1 and 13, and output the best poker hand.  (True poker experts should note that we are ignoring suits for simplicity.)

The possibilities from best to worst are: "Five of a kind", "Four of a kind", "Full house" (three cards of one kind and two of another), "Straight" (five consecutive, distinct cards), "Three of a kind", "Two pair", "Pair", and "High card" (the case in which none of the above apply).

Examples:  poker[1,2,1,2,1] should return "full house"; poker[1,1,3,4,4] should return "two pair".

Hint #1: There are many combinations of two of a kind:  the pair might be in the first two cards, the second two cards, the first and third, and so on.  To deal with this, consider sorting the five arguments before sending them to a second function with particular pattern definitions.

Hint #2:   Use multiple definitions for this second function, along with pattern matching.  Here are sample definitions for two hands.

poker[a_,a_,a_,a_,a_]:="Five of a kind"
poker[a_,a_,a_,b__]:="Three of a kind"  
   (* the name b is unnessary, although the blank-blank is not *)

poker[5,2,5,7,5]

poker[3,3,3,3,3]  (* notice the more specific of the rules is applied *)

Defaulting Functions

(a) Define a function which raises the first argument to the power specified by the second argument, unless the second argument does not appear, in which case the power should be 2.

(b) Define a function which prints the phrase "The next president of the United States will be " (note the space at the end), followed by the first argument, unless it doesn't appear, in which case it should have a default string of your choice.  (Use the Print function, and remember that to call a function with no arguments one must still provide the brackets at the end, as in "func[]".)