Clojure Assignment
1: Exercises
Fall 2010, 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.
(shallow-reverse lst)lst. For example, the list
(1 2 (3 4)) becomes the sequence
((3 4) 2 1 ) .(remove-duplicates lst)lst. For example, given (1 2 3 1 4 1 2) , remove-duplicates returns a sequence containing the elements (1 2 3 4) , in some order. (my-flatten lst)lst with all inner parentheses (or brackets) removed, returning a "flat" list of values. For example, if lst is (1 (2 3) ( ) (( )) :a) ,
the result should be (1 2 3 :a ). Hint: Use the predicate seq?. (skeleton lst)lst, but retains all the sequence structure.
For example, if lst is (1 (2 (3 4)) 5 6 (7) ( )) ,
the result is ((( )) ( ) ( )) .(deep-reverse lst)L at all levels. For example, if lst is (:a (:b :c (:d)) :e) , deep-reverse should return (:e ((:d) :c :b) :a) .(eliminate value lst)lst with all occurrences of
the value removed, at all levels. For example, if lst is (:a :b (:b :c :d (:a (:b))) :e) , (eliminate :b lst) should return(:a (:c :d (:a ())) :e). Note that the value may 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.
(zap-gremlins text)filter.(rot-13 text)(int ch) and (char n) . Hint: map.(sqrt n)n, using Newton's method. That is, choose some arbitrary number, say 2, as the initial approximation r to the square root; then to get the next approximation, compute the average of r and 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, and taking approximations until they differ by less than 0.00001. Hint: iterate.
(longest-collatz lo hi) lo to hi (including both end points) takes the longest to converge. If two values take equally long to converge, return either value.Turn in your exercises.clj file by midnight, Wednesday October 20. As only one file is required, there is no need to zip it.