CS 291 Assignment #3

Due Wednesday, February 17th (by 11:59pm)

Introduction:

Below, you'll find problems that give more practice with simple recursion (subsequence), experience with functions as inputs (sameOutput), and functions as both inputs and outputs (makeLoudVersion). The last problem is more involved — leave plenty of time to work on it, and make sure you're in your recursive happy place before starting. Style still matters: Try to be as short and concise as possible in your solutions, use pattern-matching when you can, and make an effort to take advantage of built-in functions as much as possible. And how about some comments this time? Sheesh.

The Assignment

  1. Define the function subsequence of type Eq a => [a] -> [a] -> Bool. It should return True if the first list of items is found in consecutive positions anywhere within the second list. For example:
    > subsequence [] [1,2,3]
    True
    > subsequence [1,2,3] []
    False
    > subsequence ['a', 'b'] ['a','b','c']
    True
    > subsequence [3,4] [1,2,3,4,5,6]
    True
    > subsequence [3,4] [1,2,3,5,4,6]
    False
    

  2. Define the function sameOutput with type Integral a => (a -> a) -> (a -> a) -> a -> a -> Bool. It should take two input functions, both of which map integers to integers, and check to see if they return the same outputs for all input values within a specific range of integers. (The third and fourth arguments to sameOutput specify the range of integers to be checked.) For example, the first test below asks whether the identity function and the function that squares its argument return the same output for all integer values from 0 to 1. (They do.)
    > sameOutput (\x->x) (\x->x*x) 0 1
    True
    > sameOutput (\x->x) (\x->x*x) 0 10
    False
    > sameOutput ((+1).abs) (+1) 0 100
    True
    > sameOutput ((+1).abs) (+1) (-100) 100
    False
    

  3. Define the function makeLoudVersion that takes a one-argument function as its input and returns a "loud" version of the function that maps inputs to strings as shown below. More specifically, if the input function is of type a -> b, the output function is of type a -> [Char] where the output string includes both the input and the output from the original function.
    > makeLoudVersion (+2)
    ERROR - Cannot find "show" function for:
    *** Expression : makeLoudVersion (flip (+) 2)
    *** Of type    : Integer -> [Char]
    
    > (makeLoudVersion (+2)) 5
    "fn (5) --> 7"
    > (makeLoudVersion length) [1,2,3]
    "fn ([1,2,3]) --> 3"
    > map (makeLoudVersion (+2)) [1,2,3,4]
    ["fn (1) --> 3","fn (2) --> 4","fn (3) --> 5","fn (4) --> 6"]
    

  4. Define the function phoneSpell of type [Int] -> [[Char]]. It takes a list of integers as its input, and generates all possible "words" corresponding to those numbers given the standard phone keypad mapping of letters onto the number keys. (Keys 0 and 1 have no letters, all others have three letters except 7 and 9, which have four each.) Think recursively! For example, on the [8,7,9] input below, think about what phoneSpell [7,9] would give you, and how you'd patch that up to be the right answer for [8,7,9]. You might also want to use things like map, foldr1, etc., in your solution.
    > phoneSpell []
    []
    > phoneSpell [2]
    ["a","b","c"]
    > phoneSpell [2,9]
    ["aw","bw","cw","ax","bx","cx","ay","by","cy","az","bz","cz"]
    > phoneSpell [2,0,6]
    ["am","bm","cm","an","bn","cn","ao","bo","co"]
    > phoneSpell [8,7,9]
    ["tpw","upw","vpw","tqw","uqw","vqw","trw","urw","vrw","tsw","usw","vsw","tpx",
    "upx","vpx","tqx","uqx","vqx","trx","urx","vrx","tsx","usx","vsx","tpy","upy",
    "vpy","tqy","uqy","vqy","try","ury","vry","tsy","usy","vsy","tpz","upz","vpz",
    "tqz","uqz","vqz","trz","urz","vrz","tsz","usz","vsz"]
    > phoneSpell [2,1,0,5]
    ["aj","bj","cj","ak","bk","ck","al","bl","cl"]
    > length (phoneSpell [8,7,9,3,5,7,9])
    6912
    

Submitting:

Submit the file containing your function definitions by attaching your files to an E-mail to me at brichards@pugetsound.edu. Please do not just copy and paste your solutions into the body of the E-mail! (If your E-mail client insists on including the content of text files into the body of the message, please zip the files up and attach the archive.)


Brad Richards, 2010