Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

->proj05;

// Functional Programming Practice on Clojure

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.

Accept the project invitation from GitHub Classroom here. Then, open VS Code through Coder and clone your repository. You will write all of your solutions in the provided lab05.clj file. Each exercise asks you to create a def binding with a specific name -- the starter file has placeholders for each one. When you are finished, the autograder will load your file and test each binding.

Reversing Functions

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)

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)

Misc and Functions

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

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). Make sure to include tests that demonstrate the correctness of your functions, and also include documentation for each function.

Commit and push your file to GitHub when you are finished.

Rubric

TaskPoints
my-reverse: correctly reverses top-level elements using recursion15
super-reverse: correctly reverses all nested elements using recursion15
member?: correctly returns true/false for top-level membership using recursion15
subsequence: correctly returns sublist and returns nil for invalid parameters using recursion15
Documentation: all functions and the file have descriptive comments20
Tests: -main exercises each function with varied and edge-case inputs20
Total100

Ways to lose points: