CS 214 Project 5: Functional Programming Practice


This project is to write several functions in Clojure. You may find the functions list() and cons(), and/or the empty list-literal '() to be useful in defining these functions.

Each function should be defined within the same file, and show the results of example executions in the same file.

  1. Reversing Functions.
    1. The reverse function is defined in Clojure, but write your own version using recursion called my-reverse. This function should behave like Clojure's reverse, and only reverse top-level elements. For example:
      	(my-reverse '(a b (c d) (e (f g))))
      
      should return:
              ((e (f g)) (c d) b a)
      
    2. Next, write a function super-reverse that reverses nested sublists as well as the top-level elements. For example:
      	(super-reverse '(a b (c d) (e (f g))))
      
      should return:
              (((g f) e) (d c) b a)
      
  2. Misc. Functions.
    1. Define a function member?() that behaves for a list like the built-in function contains?() behaves for a vector; it should only return true or false, and only work if the expression we're looking at is a list. Your solution must use recursion. For example,
              (member? '(1 2) '((1 2) 3 (4 (5 6))))
              (member? 3 '((1 2) 3 (4 (5 6))))
              (member? '(4 (5 6)) '((1 2) 3 (4 (5 6))))
      
      should all return:
              true
      
      and
              (member? 1 '((1 2) 3 (4 (5 6))))
              (member? 2 '((1 2) 3 (4 (5 6))))
              (member? 4 '((1 2) 3 (4 (5 6))))
      
      should all return:
              false
      
    2. Define a function (subsequence list i n) that returns the part of the input list starting at position i continuing for n elements. Your solution must use recursion. For example:
              (subsequence '(1 2 (3 4) (5 (6 7))) 1 2)
      
      should return:
              (2 (3 4))
      
      and
              (subsequence '(1 2 3 4 5 6 7) 2 4)
      
      should return:
              (3 4 5 6)
      
      If there is any issue with the parameters (e.g., the list is empty, i is an invalid position/index, or the list contains fewer than n items beyond position i), your function should return nil.
To test your functions, you should define a -main() function that "exercises" each function on lists of varying values and lengths (including empty lists).

Grading Rubric. Each function is worth 25 points, broken down as follows:

Turn in. When your functions work and pass all your tests, make a script file proj05-results in which you:

  1. Use cat to display the file containing your functions and the -main() function that tests them,
  2. Use clojure -m to run the program that tests your functions, and
  3. Enter Ctrl-d to quit script.

Submit your project by copying that single file into your personal folder in /home/cs/214/current/:

   cp proj05-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name. The grader will access and grade your project results from there.


Calvin > CS > 214 > Projects > 05 > Functional Programming Practice


This page maintained by Joel Adams.