Exam 1

Introduction to Programming Languages and Racket


CS 3540
Programming Languages and Paradigms
Spring Semester 2018


Thursday, February 1, 2018 @ 1:15 PM


Instructions



Exercises

  1. Write brief answers for each of the following items (two sentences each, max).

  2. Answer each of these questions about the structure of Racket lists.

  3. Write a Racket function named take-home-pay that takes three arguments, all numbers: an employee's hourly wage, the income tax rate, and the number of hours the employee worked this pay period. take-home-pay returns the employee's total pay after taxes have been deducted. For example:
         > (take-home-pay 10.00 0.15  6)     ; 6 hrs * $10.00/hr, minus 15%
         51.00
    
         > (take-home-pay 15.00 0.28 40)     ; a decent internship
         432.00
    
         > (take-home-pay  4.25 0.00 55)     ; minimum wage, in the old days
         233.75
    

  4. Write a Racket function named sum-of-apps that takes one arguments: a function, f, of one argument. sum-of-apps returns a two-argument function (both numbers), which applies f to both arguments and returns the sum of those values. For example:
        > (define double (lambda (n) (* n 2)))
        > (define square (lambda (n) (* n n)))
    
        > (define sum-of-squares (sum-of-apps square))
    
        > ((sum-of-apps double) 3 4)              ; (3*2) + (4*2)
        14                                        ; == 14
        > ((sum-of-apps square) 3 4)              ; 3² + 4²
        25                                        ; == 25
        > (sum-of-squares 5 12)
        169
    

  5. Suppose that we have a list containing height/weight pairs for a group of people, in inches and pounds, respectively:
         ( (76 . 195) (81 . 212) (79 . 225) (71 . 206) ... )
    
    We would like to know the average height of the people in the group who weigh more than some given amount.

    Use the higher-order functions map, apply, and filter to write a Racket function named avg-height-over that takes two arguments: a weight w and a list of height/weight pairs lst in the above form. avg-height-over finds the subset of lst who weigh more than w and returns the average height of the people in the subset. For example:
         > (avg-height-over 200 '((79 . 225))
         79
    
         > (avg-height-over  90 '((70 . 150) (62 . 100)))
         66
    
         > (avg-height-over 200 '((76 . 195) (81 . 212) (79 . 225) (71 . 206)))
         77
    
    Assume that we have already written a variable-arity function named average. You may write other helper functions if you like, but you do not have to. No recursion or looping are allowed!



Eugene Wallingford ..... wallingf@cs.uni.edu ..... January 31, 2018