Clojure
Watching Rick Hickey and trying out Clojure.
Through the labyrinth of the internet I somehow ended up watching Rich Hickey, the creator of Clojure. Now and then the stars align and I navigate Youtube, which I found to be badly sorted and organized.
It’s a great talk and it made me want to try out Clojure. My goal was to learn Swift, but, …, procrastination happened? I did a dozen or so exercises on Hackerrank.
One of the exercises was to reverse a list, without using the built-in function. Here it started to be weird.
My first thought was to use the reduce
function where you walk through the list and built up a new result. conj[oin]
is used to return a new collection with the element added.
Let’s skip reading the documentation completely of course :)
Doesn’t work:
(fn [list]
(reduce conj [] lst)
)
Works:
(fn [list]
(reduce conj () lst)
)
There is a difference based on the type ()
is a list, []
is a vector. And, as is stated clearly in the documentation conj
works differently for each datatype:
- For lists it adds at the end, the underlying implementation is a singly-linked list. So, makes sense.
- For vectors it adds at the beginning
Probably the best option is to use sequences and use cons
. But how that would exactly work…a quick try in the editor of Hackerrank just gave me stack traces that my function definition was wrong. Copy-pasting an example from the Clojure docs and the problem persisted.