Homework Assignment 4

Recursive Procedures

CS 3540
Programming Languages and Paradigms
Spring Semester 2015

Due: Thursday, February 12, at 8:00 AM


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:

Name your file of solutions homework04.rkt. The first three non-comment lines in your file should be:

    #lang racket
    (require rackunit)
    (provide the names of your five solution procedures)

The first declares that you are using the Racket language. The second enables you to use rackunit to test your solutions [ example code ]. The third exports your procedures so that I can test them using my own rackunit tests.

For each of solutions, include at least five check-equal? expressions that test it for correctness. Put your tests immediately after the procedures for the problem:

    ; main procedure
    ; helper procedures
    ; test expressions


  1. Write a recursive procedure (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* 'seattle 0)

  2. Write a recursive procedure (take n lst) that takes two arguments: a non-negative integer n and a list lst. take returns a list of the first n items in lst. For example:
         > (take 2 '(a b c d e))
         '(a b)
         > (take 5 (sequence 1 30))
         '(1 2 3 4 5)
         > (take 10 '(a b c d e))
         '(a b c d e)

  3. Write a recursive procedure (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))

  4. Write a recursive procedure (select test? lst) that takes two arguments, a procedure test? and a list lst. select returns a list of all items in lst that pass the test?. For example:
         > (select positive? '(1 -1 2 -2 3 -3 4 -4 -5))
         '(1 2 3 4)
         > (select boolean? (list 1 'a 't #t "false" #f '(a . b)))
         '(#t #f)
         > (select number? (list 'a 't #t '(1 2 3) "false" #f '(a . b)))
         > (select pair? (list 'a 't #t '(1 2 3) "false" #f '(a . b)))
         '((1 2 3) (a . b))
         > (select (lambda (n)
                     (> n 1008))
                   (sequence 1000 1010))
         '(1009 1010)

  5. Write a recursive procedure (any? arg1 arg2 ...), where the argi's are boolean values. any? returns #t if any of its arguments is #t, and #f otherwise. For example:
         > (any? #t)
         > (any? #f)
         > (any? #f #f #f #t #f #f)
         > (apply any? (map even? (sequence 1 30)))
         > (any?)

    Hint: Make any? an interface procedure that calls a helper to do the real work with a single list argument.


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

Be sure that your submission follows all of the homework submission requirements. This includes:

Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 7, 2015