One problem five solutions — palindrome
Keep going with the series where we go through multiple solutions in a few programming languages that solve the same problem in different ways, today is the day for us to analyze the palindrome problem
If you still haven't read the first article on this series about the fizzBuzz problem, please check it here!
The palindrome problem is relatively easy in complexity but very interesting to analyze, and we can define the problem as:
Create a function that given a string checks if the string is a palindrome.
A palindrome occurs when the string reversed it’s the same as its original form
ex: “radar”
With the context of the problem well-defined, we are finally able to start comparing solutions
<?php
function isPalindrome($word) {
return $word == strrev($word);
}
?>
The first solution is the PHP solution, honestly, the fact that we could use immutability in our solution and that PHP already has a built-in function for reversing a string is definitively a plus
But also I have to say that I worked with PHP for more than one year in the past, but I forgot about that ‘;’(end line symbol) that needs to be inserted on every line of code and that gave me such a frustration considering that it is a useless symbol nowadays due to the advances in compilers I hope future PHP versions turn the end line symbol optional like JavaScript for example
Talking about JavaScript, the next solution is the pure juice of the language:
const isPalindrome = word =>
word == word.split('').reverse().join('')
JavaScript doesn't have a function to reverse a string, so I had to turn the string into a list of chars, reverse this list and then join it in a string back again and compare if the reversed string is equal to the original, but the good part in this solution is that it was solved with just one line of code and using an immutable form
Another immutable solution is the Elixir solution:
defmodule Palindrome do
def is_palindrome(word), do: word == String.reverse(word)
end
I really enjoy the final Elixir solution, because the code was very clear to me and I could easily comprehend what was going on when looking at it, and also solving it with immutability that feels so natural in functional languages.
Talking about functional programming, the next solution is the Haskell solution, which I consider by far the most simple, concise, natural and comprehensible solution:
isPalindrome word = word == reverse word
To me, this solution is so beautiful and expresses the problem super well
a palindrome is a word that is reversed is equal to the original form
something is palindrome when that something is equal to this something reversed
isPalindrome when word is equal to reverse word
And the last solution is a more verbose solution but for a good reason, take a look at the V solution:
fn is_palindrome(word string) bool {
runes := word.runes()
arr_size := word.len
arr := []rune{len: arr_size, init: runes[arr_size-1 - it]} reversed_word := arr.map(it.str()).join("")
return reversed_word == word
}
The V solution was a bit more verbose, but it is because V is a super small language that is being created right now, so some things have to be done by hand
But V’s syntax is very concise and implementing things by hand looks simple, and the process was the following:
I turn the word into a list o chars (rune type in V) then I get the size of the string and build a new array of chars that is formed by the first list of chars where the element is accessed by the size of the word-1 + the current index
what makes a reversed list of chars, once I have the reversed list I just need to turn runes into a string using a map and join it very similar to the JavaScript solution
And the coolest parts were that the solution was achieved through immutability and that I had such a good time implementing something in V.
And with all the five solutions analyzed, we get to the end of one more article in this series, hope you liked it, and probably I'm going to make a short video of this article on YouTube and when it's done I will put the link on the comments
Thank you for reading. See ya!