Homework Assignment 4

Recursive Functions


CS 3540
Programming Languages and Paradigms
Spring Semester 2016


Due: Tuesday, February 16, at 8:00 AM


Introduction

This assignment asks you to write recursive procedures in Racket. The primary goal of this assignment is to gain experience with recursion and Racket lists. For this assignment:

Template Source File
Download the file homework04.rkt and use it as a template for your submission. Please keep and use the name homework04.rkt.

As before, this file includes a require clause that enables you to use Rackunit to test your solutions [ example code ]. This file also includes a provide clause that exports your five public functions. This enables me to load your module and test the functions using my own Rackunit tests.

Do Not Use...
Organizing Code



Problems

  1. Write a recursive function (symbol* sym n) that takes two arguments: a symbol sym and a non-negative integer n. symbol* returns a list consisting of n occurrences of sym. For example:
         > (symbol* 's 2)
         '(s s)
    
         > (symbol* 'ew 5)
         '(ew ew ew ew ew)
    
         > (symbol* 'carolina 0)
         '()
    

  2. Write a structurally recursive function (reject test? lst) that takes two arguments, a function test? and a list lst. reject returns a list of all items in lst that fail the test?. For example:
         > (reject positive? '(1 -1 2 -2 3 -3 4 -4 -5))
         '(-1 -2 -3 -4 -5)
    
         > (reject odd? '(1 -1 2 -2 3 -3 4 -4 -5))
         '(2 -2 4 -4)
    
         > (reject divisible-by-10       ; helpers in your
                   (sequence -1 11))     ; template file
         '(-1 1 2 3 4 5 6 7 8 9 11)
    
         > (reject pair? '(a (b c) (d e) f))
         '(a f)
    

  3. Write a recursive function (zip lst1 lst 2) that takes as arguments two lists, lst1 and lst2. zip returns a list of pairs, where each pair consists of the items at the corresponding positions in lst1 and lst2. For example:
         > (zip  '(a b c d e) '(1 2 3 4 5))
         '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5))
    
         > (zip '(foo bar baz bif) (sequence 1 4))
         '((foo . 1) (bar . 2) (baz . 3) (bif . 4))
    
         > (define 1-to-5 (sequence 1 5))
         > (zip 1-to-5 (map square 1-to-5))             ; in template file
         '((1 . 1) (2 . 4) (3 . 9) (4 . 16) (5 . 25)
    
    You may assume that lst1 and lst2 have the same length. You'll receive an extra credit point if your function handles lists of unequal lengths gracefully.

  4. Write a structurally recursive function (positions-of s los) that takes two arguments, a symbol s and a list of symbols los. positions-of returns a list containing the zero-based positions of all occurrences of s in los. For example:
         > (positions-of 'a '(a b a c d a e f g a h i j k))
         '(0 2 5 9)
    
         > (positions-of 'a '(b c d e f g h i j k))
         '()
    
    Hint: You will need to make positions-of an interface procedure that calls a helper function with a second argument.

  5. Write a structurally recursive function (every? arg1 arg2 ...), which takes any number of boolean values as arguments. every? returns #t if all of its arguments are #t, and #f otherwise. For example:
         > (every? #t)
         #t
    
         > (every? #f)
         #f
    
         > (every? #f #f #f #t #f #f)
         #f
    
         > (apply every? (map even? (sequence 1 30)))
         #f
    
         > (apply every? (map even?
                              (reject odd?
                                      (sequence 1 30))))
         #t
    
         > (every?)
         #t
    
    Hint: This may be easier if you make every? an interface procedure that calls a helper function to do the real work with a single list argument.



Deliverables

By the due time and date, submit the following files:

Be sure that your submission follows the submission requirements. Use Save Interactions As Text... to create the file of interactions that you submit, and change the file extension to txt.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 10, 2016