Fall 2012, David Matuszek
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 earlier ones depend on recursion, while the latter are best done using some of Clojure's second-order functions.
In some cases, for example reversing a list, you may find Clojure functions that already do exactly what you want. Please don't use them, but rather write the functions yourself.
You don't need higher-order functions to program these, but if you see an opportunity to use them, go ahead. Generallly, the purpose is to get you used to writing recursive functions the Clojure way.
1; else if
nis even, return collatz(
n/2); else return collatz(3
n+ 1). (Yes, this is just a complicated way to compute
(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.
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)
lstwith all inner parentheses (or brackets) removed, returning a "flat" list of values. For example, if
, the result should be
(1 (2 3) ( ) (( )) :a)
. Hint: Use the predicate
(1 2 3 :a)
lst, but retains all the sequence structure. For example, if
, the result is
(1 (2 (3 4)) 5 6 (7) ( ))
((( )) ( ) ( ))
Lat all levels. For example, if
(:a (:b :c (:d)) :e)
(:e ((:d) :c :b) :a)
(eliminate value lst)
lstwith all occurrences of the
valueremoved, at all levels. For example, if
(:a :b (:b :c :d (:a (:b))) :e)
(eliminate :b lst)should return
(:a (:c :d (:a ())) :e). Note that the
valuemay be any value, for example, a sequence.
These are all probably best done with higher-order functions, so please use them wherever they seem to work.
n, using Newton's method. That is, choose some arbitrary number, say 2, as the initial approximation
rto the square root; then to get the next approximation, compute the average of
n/r. Continue until you have five significant digits to the right of the decimal point. Do this by taking an infinite series of approximations,
(longest-collatz lo hi)
hi(including both end points) takes the longest to converge. If two values take equally long to converge, return either value.
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-shallow-reverse (is (= '(3 2 1) (shallow-reverse '(1 2 3)))) (is (= '(5 (3 4) 2 1) (shallow-reverse '(1 2 (3 4) 5)))) (is (= () (shallow-reverse ()))) ) (deftest test-deep-reverse (is (= '(3 2 1) (deep-reverse '(1 2 3)))) (is (= '(5 (4 3) 2 1) (deep-reverse '(1 2 (3 4) 5)))) (is (= () (deep-reverse ()))) ) (run-tests)
Zip and turn in your
exercises-test.clj files by 6am, Wednesday September 26 Friday, September 28. As always, only files submitted to Blackboard will be accepted.