Palindrome function

Hello,

I’m at the absolute beginning of my Clojure journey and currently following Aphyr’s turotiral (Clojure from the ground up: sequences). There is this exercise: “Write a function to find out if a string is a palindrome–that is, if it looks the same forwards and backwards.”

How I would solve it is like this:
(defn palindrome [word]
(== word (apply str (reverse word))))

but when I run it in my reply I get an error: Execution error (ClassCastException) at user/palindrome (REPL:2).

class clojure.lang.PersistentVector cannot be cast to class java.lang.Number (clojure.lang.PersistentVector is in unnamed module of loader ‘bootstrap’; java.lang.Number is in module java.base of loader ‘bootstrap’)

Can someone help me understand what I’m doing wrong? (also: is this the place to ask questions like these?)

Thank you in advance!

It works for me with = instead of ==. The official Equality guide says “Clojure’s == is intended specifically for numerical values” and “If any value being compared is not a number, an exception is thrown.”; see the Numbers section for more.

I recommend using the REPL to explore =='s behavior compared to =. For instance, try (= :foo), (== :foo :foo), and the same with "foo" instead of :foo.

In general, = is what you want in Clojure :slight_smile: :clojure:

1 Like

thanks! That’s very helpful!

2 Likes