2023 Exam Review II

Semester I Final

Vocabulary: a “higher order function” is a function that takes another function as input. You saw them in Chapter 6.

  1. (q1) Read part (b) before writing part (a)

    1. Write a very basic (“Stone age”) function that uses only recursion to join all of the words in a list together with hypens. Notice that there is no hyphen at the end.

      check_q1a = q1 ["apple","cheese","egg"] == "apple-cheese-egg"
      
    2. Instead, number each gap. If you can do this, you may skip the first part. There is no number at the end.

      check_q1b = "apple1cheese2egg3giardinera" ==
                    q1 ["apple","cheese","egg","giardinera"]
      
    3. If you used head or tail in your code, explain how you could have done without them.

  2. Duplicit y means that there are methods

     lie :: y -> Int
     cheat :: y -> y
    
    1. Write the class declaration for Duplicit.

    2. Given the declaration below, put Pos a b in the Duplicit class by using a+b for lie and switching a and b for cheat.

       data Pos = Pos Int Int
      
  3. (Randomness.) You are writing an animation that shows a random size solid blue square. The dimension of the square changes every time you click the mouse.

     data SqGame = SqGame {side :: Double, mygen :: StdGen}
     drawh :: SqGame -> SqGame
     drawh m = colored blue $ solidRectangle (side m) (side m)
    

    Write an event handler that makes the size be a random double $1\le s \le 5$.

  4. The template is a list of numbers that you think of as moving all together on the number line for example by subtracting 3 from each one.

     template = [100, 150, 120]
     result = (moveit 3 template == [97, 147, 117])
    

    The stops is a specific list of numbers that you are trying to reach with the template. For example, a stops list could be

     stops = [20, 80, 147, 190]
    

    The seeker function takes in a template and a stops list, and returns the smallest positive integer k so that at least one element of the template moved to the left k places on the number line is also in the stops list.

     seeker :: [Int] -> [Int] -> Int
     seeker template stops = 0 -- wrong
     exs = (seeker [10,29,38] [5,15,25,35] == 3)
    

    Write the seeker function.

  5. The uncurry function has a signature

     uncurry :: (a->b->c) -> (a,b) -> c
    
    1. Briefly explain the meaning of this signature.
    2. Give an example of one way this function could work.
  6. In this question, ideally you will demonstrate you understand how to use higher order functions (LYAH, Chapter 6).

    The function below takes in two positive integers $(r,s)$ with $s$ less than $r$ and produces a Pythagorean triple.

     p2A :: (Integer, Integer) -> (Integer, Integer, Integer)
     p2A (r,s) = ((r-s)*(r+s), 2*r*s, r*r+s*s)
    
     [(3,4,5),(6,8,10),(5,12,13),(8,15,17),(12,16,20),(7,24,25), ...]
    

    Using that function, do each of the following:

    1. List some Pythagorean triples: Create a list pB that has p2A applied to every integer value of $1 \le r \le 10$ and $1 \le s < r$.

    2. (p2B) Some of the Pythagorean triples come out not in ascending order, like $r=4$ paired with $s=1$ gives (15,8,17). Mathematically, the third element of the ordered triple is always greater than either of the other two. Write a function fixp2 that will put a triple in ascending order.

    3. Show how to use each of the approaches listed below to apply the fixp2 function to the elements of p2.

      1. list comprehension
      2. advanced function from chapter 6
    4. (p2C) Some of the triples have a greatest common divisor that is not 1. We want to get rid of those. Do this with a higher order function.

      A reminder that the signature of gcd is:

       gcd :: Integer -> Integer -> Integer
      
    5. Next time ask students to write the gcd function as well.

Last modified January 9, 2024: Haskell final exams. (8a53091)