Skip to content

Instantly share code, notes, and snippets.

@airhorns
Created January 13, 2011 02:48
Show Gist options
  • Select an option

  • Save airhorns/777314 to your computer and use it in GitHub Desktop.

Select an option

Save airhorns/777314 to your computer and use it in GitHub Desktop.
val d =
[(Two,Clubs),(Three,Clubs),(Four,Clubs),(Five,Clubs),(Six,Clubs),
(Seven,Clubs),(Eight,Clubs),(Nine,Clubs),(Ten,Clubs),(Jack,Clubs),
(Queen,Clubs),(King,Clubs),...] : (rank * suit) list
- dealSimple(2,3,d);
val it = [Hand ((Four,Clubs),Hand (#,#)),Hand ((Seven,Clubs),Hand (#,#))]
: hand list
-
dealSimple : int * int * deck -> hand list
dealSimple(n,k,d) = l
where n = the number of players
k = number of cards each player gets
d = deck of cards
and l = list of hands for each player
l will contain exactly n hands, for each player one hand.
dealSimple essentially gives out the cards in the deck d to
n players, such that each player has the same number of cards in
the following way: The first player gets the first k cards, the
second player gets the k+1 to 2k cards, etc.
Proceed in two steps.
1) Implement a helper function get : int * deck -> (hand * deck)
get(k,d) = (h, d') (5 points)
If k is an integer and
d is a deck
then
h is a hand containing the first k cards from the deck d
d' is the remaining deck, i.e. the deck d without the cards in the
hand h.
(* get: int * deck -> hand * deck *)
fun get (k, d:deck) = let
fun pop (0, d:deck, h:hand) = (h, d)
| pop (k:int, c::t:deck, h:hand) = pop(k-1, t, Hand(c, h))
in
pop(k, d, Empty)
end
(* dealSimple : int * int * deck -> hand list *)
fun dealSimple (n:int, k:int, d:deck) = let
fun getHand (0, _, _, l) = l
| getHand (n, k, d, l) = let
val (h, newd) = get(k, d)
in
getHand(n-1, k, newd, l@[h])
end
in
getHand(n, k, d, nil)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment