CS 291 Assignment #3

Due Monday, February 16th (by 11:59pm)

Introduction:

Below, you'll find problems that give more practice with simple recursion (subsequence), experience with functions as inputs (same_output), and functions as both inputs and outputs (make_loud_version). 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 same_output 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 same_output 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.)
    > same_output (\x->x) (\x->x*x) 0 1
    True
    > same_output (\x->x) (\x->x*x) 0 10
    False
    > same_output ((+1).abs) (+1) 0 100
    True
    > same_output ((+1).abs) (+1) (-100) 100
    False
    

  3. Define the function make_loud_version 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.
    > make_loud_version (+2)
    ERROR - Cannot find "show" function for:
    *** Expression : make_loud_version (flip (+) 2)
    *** Of type    : Integer -> [Char]
    
    > (make_loud_version (+2)) 5
    "fn (5) --> 7"
    > (make_loud_version length) [1,2,3]
    "fn ([1,2,3]) --> 3"
    

  4. Define the function phone_spell 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 phone_spell [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.
    > phone_spell []
    []
    > phone_spell [2]
    ["a","b","c"]
    > phone_spell [2,9]
    ["aw","bw","cw","ax","bx","cx","ay","by","cy","az","bz","cz"]
    > phone_spell [2,0,6]
    ["am","bm","cm","an","bn","cn","ao","bo","co"]
    > phone_spell [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"]
    > phone_spell [2,1,0,5]
    ["aj","bj","cj","ak","bk","ck","al","bl","cl"]
    > length (phone_spell [8,7,9,3,5,7,9])
    6912
    

Submitting:

Submit both the file containing your function definitions, and a script file of a Hugs98 session showing the evaluation of the sample inputs given in each problem above. (Feel free to include additional tests if you wish.) Please name both files after yourself (e.g. "richards.hs" and "richards_out.txt"). Submit by attaching your files to an E-mail to me at brichards@ups.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, 2009