Solution for the palindrome exercise in the Ninety-Nine Haskell Problems set

This is kind of silly, but I was going through the Ninety-Nine Haskell Problems exercises, and I just wanted to note that the solutions page for the palindrome exercise is missing an extremely simple and elegant point-free solution using the >>= operator:

isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome = reverse >>= (==)

While the page does include a monadic solution utilizing liftM2, it might be helpful to include this solution as well. As a newcomer, discovering this solution helped broaden my view of what monads are and how the >>= operator can be used.

5 Likes

Not silly at all, added!

99problems or project-euler are popular starting point, so any
new solution is welcome, consider
requesting an account!

1 Like

I’m still struggling with monads, list especially…

And due to this beeing pointfree I have even trouble to reverse into pointfull and then do-notation…

I think the fully expanded version would help much more.

The important part to remember is that

f >>= g

is equivalent to

\x -> g (f x) x

when f and g are functions.

So, for the solution I posted, if f is reverse and g is (==), you get:

\x -> (==) (reverse x) x

which is exactly what we want: a function that compares an input with the reverse of the input.

Hope this helps!

1 Like

Curiously, (==) <*> reverse also works. Because a == b is the same as b == a.

1 Like

Yep, that’s right! That one is on the solutions page too (named isPalindrome'''').