Docsity

Homework 4 Solved - Computer Science - Spring 2009 | CIS 252, Assignments of Computer Science

Toggle sidebar
bg1
Homework 4 CIS 252 (Spring 2009) — Introduction to Computer Science
Coverage
This assignment covers material through Chapter 7 (and also Chapter 14) of
Haskell: The Craft of Functional Programming.
Logistics
This homework is officially due in the bin in CST 3-212 by noon on Thursday,
February 19. However, it comes with an automatic extension: anything submit-
ted by noon on Friday, February 20 will be accepted as being on time.
You may work singly or in pairs on this assignment.
For each of the problems, provide: (i) a contract, (ii) a purpose statement, (iii)
examples, (iv) the definition, and (v) tests. The grade for each problem will
based on the standard homework grading criteria, see: http://www.cis.syr.
edu/courses/cis252/info/hw-policy.html#crit:hw.
Turn in a hard copy of your source code and a transcript demonstrating con-
vincingly that your code is correct. Also, submit your code (but not transcript)
electronically.
Background
In this assignment, you’ll be working with a Haskell representation of standard
playing cards and packs of these cards.
Playing cards The standard (Anglo-American-French) deck of playing cards
consists of 52 cards organized into four suits: Clubs, Diamonds, Hearts, and
Spades. Each suit has 13 cards: an Ace, a Two, a Three, .. ., a Nine, a Ten, a Jack,
a Queen, and a King. We represent suits by the enumerated type
data Suit = Clubs | Diamonds | Hearts | Spades
deriving (Eq, Ord, Enum, Show)
and represent cards with the algebraic type
data Card = Carte Suit Int
deriving (Eq)
where the Int value should be between 1 and 13 inclusive (for Aces through
Kings). So, for example, (Carte Spades 1) represents the Ace of Spades and
(Carte Hearts 12) represents the Queen of Hearts. We represent packs (i.e.,
collections of cards) with values of the type
type Pack = [Card]
where the n-th Card in the list represents the n-th card in the pack. See
http://www.cis.syr.edu/courses/cis252/code/cards.hs for a few prede-
fined Packs.
Exercises
1. Go Fish In a turn of the game of Go-Fish, the player whose turn it is chooses
the number of a card she holds (e.g., if she holds the 6 of Clubs, she might choose
6) and asks the other players if they have any cards of that number (e.g., Does
anyone have any 6’s?). All the other players give her all of their cards of that num-
ber. However, if none of the players have cards of that number, then the player
has to go fish (i.e., take the top card from the deck). In this problem, you will
work out a function to carry out a go-fish turn.
(a) Design a function
haveAny :: Int -> Pack -> Bool
such that (haveAny n cs) returns True exactly when there is a Card
in cs with that number. For example, (haveAny 6 [Carte Clubs 2,
Carte Spades 6, Carte Hearts 12]) should return True, while (haveAny
6 [Carte Clubs 2, Carte Spades 9, Carte Hearts 12]) should return
False.
(b) Design a function
pickOut :: Int -> Pack -> (Pack,Pack)
such that (pickOut n cs) returns a pair (cs’,cs’’), where cs’ is the list
of all the cards in cs with number nand cs’’ is the list of all the cards in
cs with number different than n. For example, (pickOut 6 [Carte Clubs
2, Carte Spades 6, Carte Hearts 12]) should return ([Carte Spades
6],[Carte Clubs 2, Carte Hearts 12]).
(c) Let State be the following type synonym:
type State = (Pack,Pack,Pack)
Page 1
pf2

Partial preview of the text

Download Homework 4 Solved - Computer Science - Spring 2009 | CIS 252 and more Assignments Computer Science in PDF only on Docsity!

Homework 4 CIS 252 (Spring 2009) — Introduction to Computer Science

Coverage

This assignment covers material through Chapter 7 (and also Chapter 14) of Haskell: The Craft of Functional Programming.

Logistics

This homework is officially due in the bin in CST 3-212 by noon on Thursday, February 19. However, it comes with an automatic extension: anything submit- ted by noon on Friday, February 20 will be accepted as being on time.

You may work singly or in pairs on this assignment.

For each of the problems, provide: (i) a contract, (ii) a purpose statement, (iii) examples, (iv) the definition, and (v) tests. The grade for each problem will based on the standard homework grading criteria, see: http://www.cis.syr. edu/courses/cis252/info/hw-policy.html#crit:hw.

Turn in a hard copy of your source code and a transcript demonstrating con- vincingly that your code is correct. Also, submit your code (but not transcript) electronically.

Background

In this assignment, you’ll be working with a Haskell representation of standard playing cards and packs of these cards.

Playing cards The standard (Anglo-American-French) deck of playing cards consists of 52 cards organized into four suits: Clubs, Diamonds, Hearts, and Spades. Each suit has 13 cards: an Ace, a Two, a Three,... , a Nine, a Ten, a Jack, a Queen, and a King. We represent suits by the enumerated type

data Suit = Clubs | Diamonds | Hearts | Spades deriving (Eq, Ord, Enum, Show)

and represent cards with the algebraic type

data Card = Carte Suit Int deriving (Eq)

where the Int value should be between 1 and 13 inclusive (for Aces through Kings). So, for example, (Carte Spades 1) represents the Ace of Spades and (Carte Hearts 12) represents the Queen of Hearts. We represent packs (i.e., collections of cards) with values of the type type Pack = [Card] where the n-th Card in the list represents the n-th card in the pack. See http://www.cis.syr.edu/courses/cis252/code/cards.hs for a few prede- fined Packs.

Exercises

1. Go Fish In a turn of the game of Go-Fish, the player whose turn it is chooses the number of a card she holds (e.g., if she holds the 6 of Clubs, she might choose

  1. and asks the other players if they have any cards of that number (e.g., Does anyone have any 6’s?). All the other players give her all of their cards of that num- ber. However, if none of the players have cards of that number, then the player has to go fish (i.e., take the top card from the deck). In this problem, you will work out a function to carry out a go-fish turn. (a) Design a function haveAny :: Int -> Pack -> Bool

such that (haveAny n cs) returns True exactly when there is a Card in cs with that number. For example, (haveAny 6 [Carte Clubs 2, Carte Spades 6, Carte Hearts 12]) should return True, while (haveAny 6 [Carte Clubs 2, Carte Spades 9, Carte Hearts 12]) should return False.

(b) Design a function pickOut :: Int -> Pack -> (Pack,Pack)

such that (pickOut n cs) returns a pair (cs’,cs’’), where cs’ is the list of all the cards in cs with number n and cs’’ is the list of all the cards in cs with number different than n. For example, (pickOut 6 [Carte Clubs 2, Carte Spades 6, Carte Hearts 12]) should return ([Carte Spades 6],[Carte Clubs 2, Carte Hearts 12]).

(c) Let State be the following type synonym: type State = (Pack,Pack,Pack)

Page 1

Homework 4 CIS 252 (Spring 2009) — Introduction to Computer Science

An object (p1s,p2s,ds) of type State represents the state of a two-player Go-Fish game, where p1s is the hand of the first player, p2s is the hand of the second player, and ds is the list of cards remaining in the deck. Design a function turn :: Int -> State -> State such that (turn n st) returns the state of game when player 1 begins in state st and asks for any cards with number n. There are two special cases that result in no change of state:

  • If there are no cards remaining in the deck (i.e., the two players have all the cards), then the game ends; the new state is just st.
  • If Player 1 cheats and has no cards of number n, then nothing changes in the game: that is, the new state is just st. For example: turn 6 ([Carte Spades 6, Carte Hearts 12], [Carte Diamonds 6, Carte Clubs 1], [ lots of cards ] )

should return something like ( [Carte Diamonds 6, Carte Spades 6, Carte Hearts 12], [Carte Clubs 1], [ lots of cards ] )

2. Blackjack Scoring In the game of Blackjack, a hand of cards is scored by the following rules: - A Two counts 2 points, a Three counts 3 points,... , a Ten counts 10 points, a Jack, Queen, or King counts 10 points, and an Ace you can choose to count as either 1 point or 11 points. - Given a hand of cards, you sum up the value of the cards and if the sum is 21 or lower, that is your score; if the sum is over 21, 0 is your score. - Your goal is to have the biggest score possible. So if you have at least one Ace in your hand, you choose its value accordingly.

Design a function

blackjack :: Pack -> Int

such that (blackjack hand) returns the Blackjack score of the pack hand. Hint: No matter how many Aces your hand has, you will never use more than one Ace as an 11-point card. (Why?)

3. N-way deals In this problem, you will work out a way to deal a pack of cards to n players.

(a) Design a function

distribute :: [a] -> [[a]] -> [[a]]

such that (distribute xs yss) returns the result of cons-ing the i- th element of xs onto the front of the i-th element of yss. For example, distribute [1,2,3] [[10,100],[20,200],[30,300]] returns [[1,10,100],[2,20,200],[3,30,300]]. Two important cases:

  • If xs is shorter than yss (say xs has length n), then the first n elements in yss get a card and the remaining elements of yss stay unchanged. For example (distribute [1,2] [[10,100],[20,200],[30,300]]) returns [[1,10,100],[2,20,200],[30,300]].
  • If xs is longer than yss, then the extra values in xs are not used.

(b) Design a function

deal :: Int -> Pack -> [Pack]

that, given a positive integer n and a Pack cs, returns a list of n packs that result from dealing the cards in cs into n packs. In dealing out the pack, the first card of cs should go into the first output pack, the second card of cs should go into the second output pack, and so on, in a round-robin fashion. For example,

deal 3 [Carte Hearts 3,Carte Diamonds 10, Carte Diamonds 1, Carte Clubs 4, Carte Hearts 9,Carte Spades 1]

should return

[[Carte Hearts 3,Carte Clubs 4], [Carte Diamonds 10,Carte Hearts 9], [Carte Diamonds 1,Carte Spades 1]]

If the integer n is zero or negative, then an empty list should be returned. Hint: Part (a) and the built-in functions replicate and splitAt can be very helpful here.

Page 2