There’re countries like Poland that use dates in the format ‘dd.mm.YYYY’, so today’s 21.02.2012. As pointed by @KevlinHenney on twitter it’s a palindrome and I was pleasantly surprised to find it out.
I came up with the idea of writing a Clojure function to check if a given string is a palindrome.
I remember the moment very well when I stumbled upon the (conj) function and learnt that it behaves slightly different when executed with a list or an array.
user=> ;; use conj with a list (mind the quote mark) user=> (conj '(1 2 3) 0) (0 1 2 3) user=> ;; use conj with an array user=> (conj [1 2 3] 0) [1 2 3 0]
Here’s the function I developed to check whether a given string is a palindrome or not.
user=> (defn my-reverse [s] (let [lst (list)] (reduce #(str %2 %1) (mapcat #(conj lst %1) s)))) #'user/my-reverse user=> (defn palindrome? [s] (= s (my-reverse s))) user=> (is (palindrome? "21022012")) true
I solved the problem, but with mapcat not
map. Is there a way to use just the three functions – map, reduce and filter?