Quiz 1

Introduction to Programming Languages and Racket


CS 3540
Programming Languages and Paradigms
Spring Semester 2023


Thursday, February 9, 2023 @ 1:15 PM


Instructions



Exercises

  1. Write brief answers for each of the following items. (One phrase or code expression is usually sufficient for each question.)

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

  3. Write a Racket function named line-item-cost that takes three arguments, all numbers: the number of items being purchased, the price per item, and the sales tax rate. line-item-cost returns the total cost for this purchase, including sales tax. For example:
         > (line-item-cost 1 1.00 0.05)      ; 1 x $1.00, plus 5%
         1.05
    
         > (line-item-cost 4 5.00 0.07)      ; 4 x $5.00, plus 7%
         21.40
    
         > (line-item-cost 6 10.00 0.15)     ; 6 x $10.00, plus 15%
         69.00
    
    You may write a helper function, but do not have to.

  4. Write a Racket function named fun-less-than that takes two arguments, functions f and g. Both f and g take a single integer argument.

    fun-less-than returns a function of one argument, an integer n. The returned function computes the value f(n) < g(n). For example:
        > (define double (lambda (n) (* n 2)))
        > (define square (lambda (n) (* n n)))
        > (define 2n+1   (lambda (n) (add1 (double n))))
    
        > ((fun-less-than double square) 2)  ; is (2*2) < (2*2)?
        #f
        > ((fun-less-than double square) 5)  ; is (5*2) < (5*5)?
        #t
    
        > (define 2n+1<n²? (fun-less-than 2n+1 square))
    
        > (2n+1<n²? 5)     ; because 11 < 25
        #t
        > (2n+1<n²? 2)     ; because 5 is not < 4
        #f
    
    You'll earn a bonus point if you don't use an if or a cond in your solution. Boolean values are values, too!

  5. Suppose that we have a list of pixels from an image, where each pixel is a list (r g b) containing the red, green, and blue (RGB) values of the pixel:
         ( (76 195 202) (255 71 208) (175 81 214) (0 165 81) ... )
    
    Write a Racket function named least-red-green that takes one argument, a list of pixels lst in this form. least-red-green computes the sum of the red and green values for every pixel in lst and returns the smallest of the sums. For example:
         > (least-red-green '((1 3 5)))
         4
    
         ; the red + green sums are 220 and 480, respectively
         > (least-red-green '((100 120 140) (240 240 240)))
         220
    
         ; trust me: this pixel has smallest red + green ||||||
         ;                                               vvvvvv
         > (least-red-green '((76 195 202) (255 71 208) (175 81 214)))
         256
    
    Use the higher-order functions apply and map as needed to implement your function. No recursion or looping are allowed!

    You will want to use Racket's variable-arity min function in your solution.


Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 8, 2023