Docsity

CIS 252 Homework 6: Interleaving, Copying, DNA Matching, Caesar Cipher, Assignments of Computer Science

Toggle sidebar
bg1
Homework 6 CIS 252 (Spring 2009) — Introduction to Computer Science
Coverage
This assignment focuses on material in Chapter 9 of Haskell: The Craft of
Functional Programming.
Logistics
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.
Exercises
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.
1. Fill in the blanks to write a one-line function that interleaves the
elements of two lists:
interleave :: [a] -> [a] -> [a]
interleave xs ys = concatMap (_______________) (________ xs ys)
The resulting list should be twice the length of the shorter of xs
and ys. For example, interleave "abcde" "XYZ" should return
"aXbYcZ".
2. Fill in the blanks to write a one-line function copies that, given a
positive integer kand a list xs, returns a list with kcopies of each
element of xs:
copies :: Int -> [a] -> [a]
copies k xs = ___________________________ xs
For example, copies 4 "abca" should return "aaaabbbbccccaaaa".
3. In DNA, there are four types of nucleotides (i.e., the basic build-
ing blocks), which are abbreviated A,C,G, and T. A single strand of
DNA is a sequence of these nucleotides (in any order).
DNA strands can pair up in an almost-zipper fashion to form the
well-known double helix, but only when they’re perfectly com-
plementary: they must be equal lengths, each Amust be paired
with T(and vice versa), and each Cmust match with G(and vice
versa). For example, the strand ACCTGAG can only pair up with
TGGACTC; it cannot pair up with TGGCGTC or even with itself (i.e.,
ACCTGAG).
For the questions below, you may assume that the Strings in ques-
tion contain only the characters ’A’,’C’,’G’, and ’T’. They may
be of any length (including 0).
(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
pf2

Partial preview of the text

Download CIS 252 Homework 6: Interleaving, Copying, DNA Matching, Caesar Cipher and more Assignments Computer Science in PDF only on Docsity!

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

Coverage

This assignment focuses on material in Chapter 9 of Haskell: The Craft of Functional Programming.

Logistics

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.

Exercises

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.

  1. Fill in the blanks to write a one-line function that interleaves the elements of two lists: interleave :: [a] -> [a] -> [a] interleave xs ys = concatMap (_______________) (________ xs ys)

The resulting list should be twice the length of the shorter of xs and ys. For example, interleave "abcde" "XYZ" should return "aXbYcZ".

  1. Fill in the blanks to write a one-line function copies that, given a positive integer k and a list xs, returns a list with k copies of each element of xs:

copies :: Int -> [a] -> [a] copies k xs = ___________________________ xs

For example, copies 4 "abca" should return "aaaabbbbccccaaaa".

  1. In DNA, there are four types of nucleotides (i.e., the basic build- ing blocks), which are abbreviated A, C, G, and T. A single strand of DNA is a sequence of these nucleotides (in any order). DNA strands can pair up in an almost-zipper fashion to form the well-known double helix, but only when they’re perfectly com- plementary: they must be equal lengths, each A must be paired with T (and vice versa), and each C must match with G (and vice versa). For example, the strand ACCTGAG can only pair up with TGGACTC; it cannot pair up with TGGCGTC or even with itself (i.e., ACCTGAG). For the questions below, you may assume that the Strings in ques- tion contain only the characters ’A’, ’C’, ’G’, and ’T’. They may be of any length (including 0).

(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.

  1. The Caesar cipher—named after Julius Caesar, who supposedly in- vented it—is a simple (and rather insecure) way to encrypt infor- mation. The idea is that each letter in a message is “shifted” by a certain amount: for example, if the shift value is 2, then the letter ’A’ is replaced by ’C’, and the letter ’Y’ is replaced by ’A’. Note: You may find the discussion of ord and chr on page 42 of the textbook useful. You may also wish to make use of functions in Haskell’s Char module (be sure to remember to import Char at the beginning of your file).

(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