Docsity

Sample Examination 2 - Introduction to Computer Science | CIS 252, Exams of Computer Science

Toggle sidebar
bg1
1. (24 points) Suppose that the following definitions are made:
isVowel :: Char -> Bool
isVowel c = elem (toLower c) "aeiou"
nonpos :: Int -> Bool
nonpos m = m <= 0
listify :: a -> [a]
listify x = [x]
h :: Int -> Char -> Char
h 0 ch = โ€™aโ€™
hnch=ch
(a) (10 points) Give the values of the following expressions.
(i). map listify [โ€™dโ€™,โ€™iโ€™,โ€™sโ€™,โ€™cโ€™]
[[โ€™dโ€™],[โ€™iโ€™],[โ€™sโ€™],[โ€™cโ€™]] or ["d","i","s","c"]
(ii). filter isVowel "travel"
"ae"
(iii). (length . listify) [1,2,3]
1
(iv). zipWith h [0,2,4] "bid"
"aid"
(v). foldr h โ€™Xโ€™ [5, 10, 15, 20]
โ€™Xโ€™
(b) (14 points) Give the types of the expressions below (e.g., [False] has [Bool]).
(i). Just โ€™Mโ€™
Maybe Char
(ii). filter nonpos [3,-17,31]
[Int]
(iii). [isVowel, isAlpha, isDigit]
Char -> Bool
(iv). (listify . nonpos)
Int -> [Bool]
(v). map h
[Int] -> [Char -> Char]
(vi). fst (listify, isVowel)
a -> [a]
(vii). foldr h
Char -> [Int] -> Char
pf3
pf4
pf5

Partial preview of the text

Download Sample Examination 2 - Introduction to Computer Science | CIS 252 and more Exams Computer Science in PDF only on Docsity!

  1. (24 points) Suppose that the following definitions are made:

isVowel :: Char -> Bool isVowel c = elem (toLower c) "aeiou"

nonpos :: Int -> Bool nonpos m = m <= 0

listify :: a -> [a] listify x = [x]

h :: Int -> Char -> Char h 0 ch = โ€™aโ€™ h n ch = ch

(a) (10 points) Give the values of the following expressions. (i). map listify [โ€™dโ€™,โ€™iโ€™,โ€™sโ€™,โ€™cโ€™] [[โ€™dโ€™],[โ€™iโ€™],[โ€™sโ€™],[โ€™cโ€™]] or ["d","i","s","c"] (ii). filter isVowel "travel" "ae" (iii). (length. listify) [1,2,3] 1 (iv). zipWith h [0,2,4] "bid" "aid" (v). foldr h โ€™Xโ€™ [5, 10, 15, 20] โ€™Xโ€™ (b) (14 points) Give the types of the expressions below (e.g., [False] has [Bool]). (i). Just โ€™Mโ€™ Maybe Char (ii). filter nonpos [3,-17,31] [Int] (iii). [isVowel, isAlpha, isDigit] Char -> Bool (iv). (listify. nonpos) Int -> [Bool] (v). map h [Int] -> [Char -> Char] (vi). fst (listify, isVowel) a -> [a] (vii). foldr h Char -> [Int] -> Char

  1. (16 points) For each of the following tasks, assume that you start out in your home directory (thus, each task is completely independent of the others). What one-line Unix command (possibly involving pipes and filters) should you type to perform each of the following tasks?

(a) List all transcript files (i.e., with the extension .trans) in your home directory that were last modified in February (hint: use the string โ€™Febโ€™) Answer: ls -l .trans | grep โ€™Febโ€™ (b) Find all words in /usr/dict/words that contain exactly three characters Answer: grep โ€™^...$โ€™ /usr/dict/words (c) Count the number of lines in the file tyrants that contain at least two occurrences of the (lowercase) letter โ€™yโ€™ Answer: grep โ€™y.yโ€™ tyrants | wc -l (d) Find all words in /usr/dict/words that begin with (lowercase) โ€™sโ€™ and do not end with (lowercase) โ€™nโ€™ Answer: grep โ€™^sโ€™ /usr/dict/words | grep -v โ€™n$โ€™

  1. (10 points) Write a Haskell function

liftMap :: (a -> b) -> ([[a]] -> [[b]])

such that liftMap f returns a function with the following behavior:

It takes a list of lists and applies f to each item in each list in.

Answer:

liftMap f [] = [] liftMap f (xs:xss) = (map f xs) : liftMap f xss

Another option:

liftMap f = map (map f)

receives :: Card -> Int

such that (receives card) returns the number of extra cards the nondealer re- ceives for the face-up card. For example, receives (Std Hearts (Face โ€™Qโ€™)) should return 2. Answer:

receives :: Card -> Int receives (Colon _) = 0 receives (Std suit _) = cards suit where cards :: Suit -> Int cards Diamonds = 1 cards Hearts = 2 cards Spades = 3 cards Clubs = 4

(b) (12 points) One way is to determine the relative strength of a Bridgette hand is to count the number of high-card points: each Ace counts as 4, each King as 3, each Queen as 2, and each Jack as 1; all other cards count 0. For example, a hand containing two Aces, a King, and three Queens has (2 ร— 4) + (1 ร— 3) + (3 ร— 2) = 17 high-card points.

Write a Haskell function

highCardPts :: [Card] -> Int

such that (highCardPts hand) returns the number of high-card points in hand. For example, highCardPts [Std Spades (Face โ€™Jโ€™), Std Clubs (Spot 10), Colon Grand, Std Spades Ace, Std Hearts (Face โ€™Jโ€™)] should return 6.

Answer:

highCardPts :: [Card] -> Int highCardPts hd = sum (map count hd) where count :: Card -> Int count (Std _ Ace) = 4 count (Std _ (Face โ€™Kโ€™)) = 3 count (Std _ (Face โ€™Qโ€™)) = 2 count (Std _ (Face โ€™Jโ€™)) = 1 count _ = 0

  1. (12 points) A binary tree is either an empty tree or a node with two children, each of which is also a binary tree. As we saw in lecture, we can define (polymorphic) binary trees in Haskell using the following type definition:

data BTree a = Empty | BNode a (BTree a) (BTree a) deriving (Show)

Write a Haskell function

revTree :: BTree a -> BTree a

such that (revTree tree) reverses the tree tree by swapping each nodeโ€™s left and right children. Answer:

revTree Empty = Empty revTree (Bnode item lc rc) = BNode item (revTree rc) (revTree lc)