Update On Learning Clojure

I am getting back to learning Clojure. I got Carin Meir’s Living Clojure.

The last section is a ten-week learning program. When I first read that, I got excited. But then I got to that part, and I was less excited. The first part is 4Clojure problems. I have tried starting it a few times, and I keep randomly scanning the Clojure cheat sheet and just throw functions together. I don’t feel like I am learning anything.

I got stuck on problem 30. Here is the answer I tried:

(reduce (fn [r x]
    (println "r is " r ", x is " x "; last element of r is " (last r))
    (if (= (last r) x) 
        r 
        (conj r x)))
        []
        [1 1 2 3 3 2 2 3] )

(The last vector is one of the problem inputs.)

I got it to work, but I kept getting vectors, but not lists, which is what 4Clojure wanted.

I did search for some answers. There are a few github repositories with 4Clojure answers. The “provided” answer uses the “map” function. I kept trying “reduce”. “map” returns a result that is the same size as the input, while “reduce” can change the size of the collection. I thought that was the way to get the answer, since the name of the problem is “Compress a Sequence”.

I suppose I could have added something to change the result to return a list, but that just seemed way too complicated. So I caved.

2016-05-04_01.10.02 postscript: I admit I used the provided solution to problem 30. A lot of the other users whose solutions I track had more complicated solutions than the publicly available one, and they looked more like the one I was trying.

But their solutions to 31were like the “provided” solution to 30.

2016-05-04_03.52.13 postscript: I might be using some of the answer repos a lot. I got a solution for problem 33 that works in my repl fine for all of the problems in 33. But online it chokes on the third one for some reason.

I got a tip from Norm Richards to set my solution to “__”. This allows you to run the examples right in the repl. Here is my solution that works on my laptop, but not on 4Clojure.com:

(def __ #(apply interleave (take %2 (repeat %1))))

(= (__ [1 2 3] 2) '(1 1 2 2 3 3))

(= (__ [:a :b] 4) '(:a :a :a :a :b :b :b :b))

(= (__ [4 5 6] 1) '(4 5 6))

(= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4]))

(= (__ [44 33] 2) [44 44 33 33])
(def __ #(apply interleave (take %2 (repeat %1))))

(= (__ [1 2 3] 2) '(1 1 2 2 3 3))

(= (__ [:a :b] 4) '(:a :a :a :a :b :b :b :b))

(= (__ [4 5 6] 1) '(4 5 6))

(= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4]))

(= (__ [44 33] 2) [44 44 33 33])

I seem to get a lot of solutions that will work for all but one of the problems.

You’re welcome.

Image from Gospel Book from the Bamberg Cathedral, an early 11th century manuscript, manuscript housed at Bavarian State Library, webpage information here, image from World Document Library, image assumed allowed under Fair Use.

1 thought on “Update On Learning Clojure”

  1. Great post. I used to be checking constantly this blog and I’m impressed!
    Extremely helpful information specially the final
    section 🙂 I deal with such information much.
    I was seeking this certain info for a long time.

    Thank you and best of luck.

Comments are closed.