Homework Assignment 4

Recursive Procedures


CS 3540
Programming Languages and Paradigms
Fall Semester 2011


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


Introduction

This assignment is the first that requires you to write recursive procedures in Scheme. The primary goal of this assignment is to gain experience with recursion and the Scheme primitive types, so you may not use the built-in procedures map, apply, list->vector, or vector->list within the body of any of your solutions. But I encourage you to explore other approaches to these problems -- that's a great way for you to master higher-order procedures and the other new ideas we are studying.



Problems

  1. Write a procedure (drop n lst) that takes two arguments: a non-negative integer n and a list lst. drop returns a list of all the items in lst except the first n. For example:
         > (drop 2 '(a b c d e))
         (c d e)
    
         > (drop 25 (sequence 1 30))
         (26 27 28 29 30)
    
         > (drop 0 '(a b c d e))
         (a b c d e)
    
         > (drop 5 (sequence 1 5))
         ()
    

  2. Write a procedure (any? lst), where lst is a list of boolean values. any? returns #t if any member of lst is #t, and #f otherwise. For example:
         > (any? '(#t))
         #t
    
         > (any? '(#f))
         #f
    
         > (any? '(#f #f #f #t #f #f))
         #t
    
         > (any? '(#f #f #f #f))
         #f
    
         > (any? '())
         #f
    

  3. Write a procedure (count sym los), where sym is a symbol and los is a list of symbols. count returns the number of occurrences of sym in los. For example:
         > (count 'a '(a b a c d a e f g a h i j k))
         4
    
         > (count 'a '(b c d e f g h i j k))
         0
    

  4. Write a procedure (detect test? lst) that takes two arguments, a procedure test? and a list lst. detect returns the first item in lst that passes the test?. If no item passes the test, detect returns false. For example:
         > (detect positive? '(1 -1 2 -2 3 -3 4 -4 -5))
         1
    
         > (detect boolean? (list 1 'a 't #t "false" #f '(a . b)))
         #t
    
         > (detect number? (list 'a 't #t '(1 2 3) "false" #f '(a . b)))
         #f
    
         > (detect pair? (list 'a 't #t '(1 2 3) "false" #f '(a . b)))
         (1 2 3)
    
         > (detect (lambda (x)
                     (not (number? x)))
                   (list 1 'a 't #t '(1 2 3) "false" #f '(a . b)))
         a
    
         > (detect (lambda (n)
                     (> n 1008))
                   (sequence 1000 1010))
         1009
    

  5. Write a procedure (positions-of s los), which takes arguments, a symbol s and a list of symbols los. positions-of returns a list containing the zero-based positions of the 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))
         ()
    
         > (= (count 'a '(a b a c d a e))
              (length (positions-of 'a '(a b a c d a e))))
         #t
    


Deliverables

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

Be sure that your submission follows all homework submission requirements.



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