Write and test the following functions. Please be sure to get the spelling and capitalization right, and the right number and types of parameters, in order to make testing them feasible.
These are mostly unrelated exercises, to give you some experience with Clojure syntax. The exercises in the first group do not require higher-order functions, the exercises in the second group might be done with higher-order functions, and the exercises in the third group are higher-order functions. Almost all are recursive.
In some cases, for example reversing a list, you may find Clojure
functions that already do exactly what you want. (This is certainly true
for the functions whose names begin with
my-.) Please don't
use them, but rather write the functions yourself.
Most of these functions are defined to work with lists, because my experience in Lisp has been almost entirely with lists. If your functions works with lists, they will probably also work with other kinds of sequences.
In all cases, assume that the inputs to the function are correct; don't do any error checking.
You don't need higher-order functions to program these, but if you see an opportunity to use them, go ahead. Generally, the purpose is to get you used to writing recursive functions the Clojure way.
vis not any kind of a collection (check with
(member? x lst)
lst. For example,
has 3 top level elements;
(a (b c (d e)) f)
is a single top-level element.
(b c (d e))
has 4 top-level elements.
(a b () d)
(append lst1 lst2)
lst2into a single list. For example, if
(:a :b :c)and
(1 (2 3)), the result should be
(:a :b :c 1 (2 3)).
(zip lst1 lst2)
lst2into a list of two-element lists; stop when you run out of elements in either list. For example, if
(:a :b :c)and
(1 (2 3) 4 5), the result should be
((:a 1) (:b (2 3)) (:c 4)).
(lookup key list-of-pairs)
keyand a list of
(key value)pairs, return the
valuethat corresponds to the
nilif there is no such pair. You can assume that there is only one such pair.
(my-merge lst1 lst2)
(3 7 12 19 19 25 30)and
(4 7 10 12 20), the result should be
(3 4 7 7 10 12 12 19 19 20 25 30).
lst, at all levels. For example,
(a (b c () (25) nil) ())
(my-drop n lst)
lstwith the first
nelements removed. For example,
(my-drop 3 '(a b c d e))should return
(d e)). If
nis equal to or greater than the length of the list
lst, return the empty list,
(my-take n lst)
lsthas fewer than
nelements, the result is just
. Strong hint: Use two parameters lists, the second one using an "accumulator" parameter to collect the elements taken from
lst. You are likely to find
reverseto be useful.
lst. For example, the list
becomes the sequence
(1 2 (3 4))
((3 4) 2 1)
lst. For example, given
(1 2 3 1 4 1 2)
remove-duplicatesreturns a sequence containing the elements
, in some order.
(1 2 3 4)
, the result should be
(((1 1) (2 3)) ((5 7)))
((1 1) (2 3) (5 7))
These are best done with higher-order functions, so please use them wherever they seem to work.
7, has been replaced by
(seq string)returns a list of characters, and
n, returns the divisors of
n, other than 1 and
nitself. For example, 12 has divisors (
2, 3, 4, 6). Hint: Use
list-of-strings; if there is more than one longest string, return the earlier one. Assume that
list-of-stringsis not empty. Hint:
(my-map f lst)
to each element of
lst, returning a list of the results.
(my-filter pred lst)
predto each element of
lst, returning a list of the ones that pass the test.
value?and the first element of the sequence (if
value?is present), else to the first two elements of the sequence; applies the function to the result and the next element in the list, recursively. Hint: Since this function takes two or three arguments, you have to use the proper syntax for this. See Defining and calling functions in my Concise Guide to Clojure.
(my-flat-map f lst)
fmust be a function that returns lists.
my-flat-mapapplies the function
fto each element of
lst, flattens the resultant list by removing one level of parentheses, and returns the result.
Write, in a separate file, unit tests for all your methods. Since we
haven't talked about testing yet, here's a model for you to follow. Notice
the use of
(ns user (:use clojure.test)) (deftest test-my-reverse (is (= '(3 2 1) (my-reverse '(1 2 3)))) (is (= '(5 (3 4) 2 1) (my-reverse '(1 2 (3 4) 5)))) (is (= () (my-reverse ()))) ) (run-tests)
Zip and turn in your
exercises-test.clj files by 6am, Tuesday, September 27
As always, only files submitted to Canvas
will be accepted. I do not accept assignments submitted by email.