# My solutions for the first 50 problems on 4clojure.com

### Problem 21

I checked the code of drop and this is the solution I used.

(fn element-at [list n]
(let [go (fn [list n] (if (and (pos? n) list) (recur (rest list) (dec n)) list))]
(first(go list n))))

### (slightly pedantic) bug in solution #30

Here's the bug:

user=> (cmprs [1 1 2 2 nil nil])(1 2)

Because (first r) returns nil if r is emtpy, any trailing nils in the input collection will be lost. This fixes it:

(defn cmprs2 [coll]   (when-let [[f & r] (seq coll)]    (if (and (nil? f) (empty? r))      (list f)       (if (= f (first r))        (cmprs2 r)        (cons f (cmprs2 r)))))) user=> (cmprs2 [1 1 2 2 nil nil])(1 2 nil)

### a simpler solution for

a simpler solution for #28:
(fn flat [x] (apply concat (map #(if (sequential? %) (flat %) [%]) x)))

### Problem 44

I have a solution that I believe to be just a bit more concise than the previous comment.

(fn rotate[n seq]  (let [n (mod n (count seq))]    (concat (drop n seq) (take n seq))))

### Problem 27

27. (fn [what] (= (reverse what) ((comp reverse reverse) what)))

### For program 40

#(mapcat vector (repeat (count %2) %1) %2)

You can use your previous solution, and mapcat (or interleave) 2 seqs.

### Sorry, the correct and better way:

#(drop-last (interleave %2 (repeat (count %2) %1)))

### Problem 30 - compression

#(map first (partition-by identity %))

partition-by identity separates into a seq separated by sections of elements. then you get the first of each section and return the seq.

### Problem 27 - palindrome?

(fn [x] (= (reverse x) (seq s)))

### Problem 26 - fibonacci

Mine is a bit more verbose...
(fn [ind] (map (fn fib [x] (if (<= x 2) 1 (+ (fib (- x 1)) (fib (- x 2))))) (map #(+ 1 %) (range ind))))

Basically, make a fibonacci function, map it to a range offset by 1.

### For exercise number 21... could be done shorter

#(.get %1 %2)

#( ( vec %) %2)

### Neat

This solution takes advantage of the fact that a Clojure vector is a function. This function returns an element of the vector with the given index.

Thanks for contributing this one. Love it.

### problem #21 - your solution

problem #21 - your solution is confusing..simple one below

(fn[col x ]
(first (drop x col)))

### duplicate

citizen428 has already posted the same solution. Thank you for stopping by.

### I wish they would put some of these up alongside the problems

I really love how elegantly you have solved these solutions, far better than my attempts (the functional way of thinking still makes my brain hurt) Thanks for taking the time to document your thought processes along with the problems. I've found it more useful than reading documentation as the problem domain is more succinct.

Thanks!

### Problem 27

Solution to the problem 27 is as simple as:
#(= (seq %) (reverse %))

### Good one

This is a nice example of the difference between functional and imperative thinking. Here the thinking is on the collection level,  not element-wise comparison, like an imperative programmer would do.  I guess it takes a lot to unlearn twenty years of imperative programming

### The solution to problem 21

The solution to problem 21 seems a bit too complicated:

(fn [coll n] (first (drop n coll)))

### agreed

You are right, that solution is too complicated. When I wrote that, I had not been used to the functional way of thinking, which is more high level and declarative. As you can see, that solution was still trying to do things sequentially, basically trying to do a loop by function composition. Your solution is more declarative: just drops the last n elements and does not care about how they are dropped.

Another lesson is that one really needs to get acquainted with seq library of Clojure. I did not know about the drop function when I wrote my solution.

Thanks for sharing your solution. I appreciate it.

### Problem 44

I struggled with that one a lot. My first solution was about twice as long as yours. Then I figured out it was simply a matter of splitting the collection in half at the right place, then joining the two pieces back together in the opposite order.

#(let [        [l r] (split-at (mod % (count %2)) %2)]    (concat r l))

### very nice

Yours is short and sweet. I didn't think in that way. split-at is good to know too. Thank a lot.

## Post new comment

• Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <h1><h2><h3><sub><sup><b><i><u><font><img>
• You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
• Lines and paragraphs break automatically.