



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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
(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$โ
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
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)