How to remember the difference between conj and cons in Clojure
When I started writing Clojure, I couldn’t memorize the difference between
cons and always used one instead of the another. Their name are
cons is used to add an element at the beginning of a vector,
conj is used to add an element at the end of it. How can one memorize
this? I found a mnemonic trick over the time that helps me remember this. Here
The trick is to look at the last letter of each function,
j. As shown
in the image below, the
cons shows the right, while the
conj shows the left.
This means that
cons pushes elements from the left to the right, that is,
at the beginning of a vector.
conj, on the other hand, pushes elements
from the right to the left, which is at the end of a vector. That’s it.
Once you see this in your head, you’ll never forget the difference between
conj on a vector.