Ch 02 Quiz 3 (Test)

Use lower case letters for all of the words in this test.

  1. Edit the following code so it creates points on the graph of $y = (3x-1)^2-(3x-1)+1$.

     em xs = [ (x,z^2-z+1) | z <- 3*x-1, x <- [xs]]
     em [0..3] == [(0,3),(1,3),(2,21),(3,57)]
    
  2. According to Haskell, how many elements are in this list? Explain.

    trick = [2^2, 3^2..6^2]
    
  3. (vowelStart): In Pig Latin, words are handled differently depending on whether they start with a consonant or a vowel. Write a function to return True when its input word starts with a vowel.

    vowelStart "queen" == False
    vowelStart "amber" == True
    
  4. (Pig Latin vowels.) In Pig Latin, when a word starts with a vowel (aeiou) the “encoded” version of it just has “way” on the end. Assuming the given word starts with a vowel, produce the proper Pig Latin version. It does not matter what your function does if the word does not start with a vowel.

    pigLatinV "amber" == "amberway"
    pigLatinV "ebony" == "ebonyway"
    pigLatinV "iron" == "ironway"
    pigLatinV "ouchie" == "ouchieway"
    
  5. (Pig Latin consonants.) In Pig Latin, when a word starts with a consonant then that consonant moves to the end of the word and you add “ay”. (Technically, it’s more complicated, but ignore the “advanced” rules if you know them.)

    pigLatinC "horse" == "orsehay"
    pigLatinC "charlie" == "harliecay" -- really should be "arliechay"
    

    It does not matter what your function does if the word does not start with a consonant.

  6. (Piggy) Given a list of words, produce a list containing first all of the words that start with a vowel, encoded into Pig Latin, followed by all of the words that start with a consonant, encoded into Pig Latin.

    piggy ["steel","bridge","across","undulating","water"] ==
      ["acrossway","undulatingway","teelsay","ridgebay","aterway"]
    
  7. (pu) Sam wants to take in a list of decimal numbers and pair up (pu) each one with a multiple of 7 according to the location the number appears in the list.

    pu [3.14, 7.28, 10.03, 19.95] == [(0,3.14),(7,7.28),(14,10.03),(21,19.95)]
    

    The plan is to use zip with the list [0,7..]. Will this work or will there be a problem? Explain.

  8. (Trace recursion.) Write down every call to the tr function that occurs when evaluating y. Include the function’s input and its output.

    tr [] = 10
    tr [x] = 5
    tr xs  = head xs * tr (tail (tail xs))
    y = tr [2, 7, 20]
    
  9. (almostOn) Given a list of integer coordinate points, return those points that are on the curve $y=(x-7)^2-50$. Also return points where the y coordinate is within 1 of the correct one. For example, $(7,-49)$ should be included because its y coordinate within 1 of the correct one (when $x=7$, the curve has $y=-50$).

     almostOn [(7,-49),(0,0),(15,14),(8,3)] == [(7,-49),(0,0),(15,14)]
    

Programming on Computer

10. first index in

(fii) The “first index in” function (fii) takes in a sentence and a letter, and outputs the number corresponding to the first location of the letter in the string, counting from 0.

  1. Write the signature for fii.
  2. Assuming that the letter appears in the string, write fii.
  3. How would you modify your code to return -1 when the letter is not in the string? Rewrite it or explain in a sentence.

Examples:

       fii "alphabet soup" 'a' == 0
       fii "alphabet soup" 'p' == 2
       fii "alphabet soup" 'x' == (-1)

There may be a built-in function to do this. Even if you know it you cannot use it.

11. maximum adjacent product distance

Define the maximum adjacent product distance (mapd) to be the difference between the greatest and least product of two adjacaent numbers in a list.

In this example we use a step by step method to compute mapd [10,2,5,18].

  1. Compute the products of all of the adjacent pairs of numbers
    • 10*2 = 20
    • 2*5 = 10
    • 5*18 = 90
  2. Find the greatest and least of those: 90 and 10.
  3. Find the difference: 90-10 = 80

Therefore, mapd [10,2,5,18] == 80.

Your job is:

  1. Write the signature of mapd.
  2. Write the complete mapd function.

Any method is acceptable. You do not need to use the method above.