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

#( ( 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 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.