

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!


Homework 6 CIS 252 (Spring 2009) — Introduction to Computer Science
This assignment focuses on material in Chapter 9 of Haskell: The Craft of Functional Programming.
This homework is officially due in the bin in CST 3-212 by noon on Thurs- day, March 19. However, it comes with an automatic extension: anything submitted by noon on Friday, March 20 will be accepted as being on time.
You may work singly or in pairs on this assignment.
As always, you’re expected to follow the design recipe discussed in lec- ture.
Turn in a hard copy of your source code and a transcript demonstrating convincingly that your code is correct. Also, submit your code (but not transcript) electronically.
Before you start writing code, you may find it useful to review the built- in higher-order functions discussed in lecture: it always helps to know which tools you have in your arsenal.
For this assignment, you are not allowed to use list comprehensions: instead, you should use map, filter, or some other higher-order func- tion as necessary.
The resulting list should be twice the length of the shorter of xs and ys. For example, interleave "abcde" "XYZ" should return "aXbYcZ".
copies :: Int -> [a] -> [a] copies k xs = ___________________________ xs
For example, copies 4 "abca" should return "aaaabbbbccccaaaa".
(a) Fill in the blanks to write a function dnaMatch that, given a strand of DNA, returns the complementary strand: dnaMatch :: String -> String
dnaMatch strand = map match strand where pairs = zip "ACGT" ________ match :: _______________________ match _____ = _____ (______ (filter _____________ pairs)) For example, dnaMatch "ACCTGAG" should return "TGGACTC". (b) Write a function zipDNA :: String -> String -> Maybe [(Char,Char)]
Page 1
Homework 6 CIS 252 (Spring 2009) — Introduction to Computer Science
such that (zipDNA s1 s2) zips together the strands s1 and s2, assuming they’re perfectly complementary. Otherwise, your function should return Nothing.
(a) Write a function shift :: Int -> Char -> Char such that (shift k c) returns the result of shifting the char- acter c by k. Your function should always stay in the same case (i.e., lowercase letters are shifted to lowercase, and capitals to capitals); non-alphabetic characters should be unchanged by shifting. For example, shift 2 ’a’ returns ’c’, shift 2 ’Z’ returns ’B’, and shift 2 ’!’ returns ’!’. (b) Fill in the blanks to write a one-line function caesar that, given an integer and a string, encrypts the string by shifting all let- ters by the indicated amount: caesar :: Int -> String -> String
caesar k cs = _________________________ cs For example, (caesar 3 "A mouse, Zeke thought, might be around...") returns "D prxvh, Chnh wkrxjkw, pljkw eh durxqg...".
(c) Fill in the blanks to write a one-line function caesar2 that does a similar thing as caesar, except that all punctuation and spaces are deleted and all characters (regardless of case) are converted into uppercase: caesar2 :: Int -> String -> String
caesar2 k cs = _________________________ cs For example, (caesar 3 "A mouse, Zeke thought, might be around...") returns "DPRXVHCHNHWKRXJKWPLJKWEHDURXQG".
Page 2