Clojure/Scheme/Racket Assignment
Create the following functions using Clojure or Racket. (Both Racket and Clojure are closely related languages derived from Lisp – the original functional language.)
Put all the functions together in a single file. Drop the file in CougarVIEW (WebCT). I recommend using Racket(Scheme) for several reasons: 1) There is a nice IDE for Racket/Scheme called Dr. Racket, 2) There are some nice Scheme tutorials available, 3) I have a series of videos on Scheme and one particular video on this assignment, and 4) I find the execution of Clojure on the JVM a bit pokey. If you prefer to use Clojure, that will be fine, too.
You can download the Dr. Racket IDE here:
You can find a lecture on this assignment here: Even if you use Clojure, you may find this lecture helpful.
- Function 1 Write a function called make-seq-list which accepts two parameters (x y) and returns the list containing x, x+1, x+2,...,y. You can assume that x and y are integers and x < y.
Example: (make-seq-list 1 5) returns (1 2 3 4 5)
- Function 2 Write a function called make-whole-list which accepts one parameter (x) and returns a list containing 1, 2, 3, ..., x. You may assume that x is an integer and 1 < x.
Example: (make-whole-list 4) returns (1 2 3 4)
- Function 3 Write a function called find-ith-element which accepts a list as the first parameter and an integer, say i, as the second parameter, and which returns the element in the ith location of the given list. Return false if the list does not have an ith element.
Example: (find-ith-element (list 8 9 6 3) 3) returns 6
(find-ith-element (list 8 9 6 3) 10) returns false
- Function 4 Write a function called last-element which accepts a list as a parameter and which returns the last element in the list. You may assume that the list is non-empty.
Example: (last-element (list 1 2 3 4 5)) returns 5
- Function 5 Write a function called list-length which accepts a list as a parameter and which returns the number of items in the list. The function should work for empty lists.
Example: (list-length ‘(1 2 3 4 5)) returns 5
- Function 6 Write a function called concat-list which accepts two lists and that returns the list derived by concatenating the lists into one list. For example (concat-list (list 1 2 3) (list 4 5) ) would return (list 1 2 3 4 5). Do not use the "append" function.
Example: (concat-list ‘(1 2 3) ‘(4 5)) returns (1 2 3 4 5)
- Function 7 Write a function called reverse-list which accepts a list as a parameter and that returns the original list in reverse order.
Example: (reverse-list ‘(1 2 3 4)) returns (4 3 2 1)
- Function 8 Write a min-element and a max-element function. Each function is passed a list of numbers and returns the appropriate number. You may assume that the parameter list is non-empty.
Example: (min-element ‘(3 2 5 9) returns 2
(max-element ‘(3 2 5 9) ) returns 9
- Function 9 Write an even and an odd function. Each function is passed a list. The even function returns the list of elements at positions 2, 4 , 6, ... as a list. The odd function returns the list of elements at positions 1, 3, 5, ... as a list.
Example: (even ‘(10 3 5 7 9 8 4)) returns (3 7 8)
(odd ‘(10 3 5 7 9 8 4) returns (10 5 9 4)
- Function 10 Write a function merge which is passed two lists (representing sorted lists) and that returns the single list obtained by merging the given lists. For example, (merge (list 1 4 5 9 19) (list (2 6 7 10)) returns the list (1 2 4 5 6 7 9 10 19).
Example: (merge ‘(1 5 9 12) ‘(2 3 4 10)) returns (1 2 3 4 5 9 10 12)