For our next recursive example, we're going to take a look at what a palindrome is.
You might already be familiar with what a palindrome is, but in case you're not, it is a sequence
that reads the same from left to right and right to left.
So we're going to have a function that's going to return true if it is a palindrome and false if it's
not.
So it's going to be Boolean.
So if we look at this sequence, we have one, two, three, four, which we can easily tell that it's
not the same from left to right as it is from right to left.
So it's not going to be a palindrome.
This sequence, however, we see we have one, two, three, four, three, two, one.
So we see we have one and one, two and two, three and three, and then four is in the middle.
So that's always going to be there.
Therefore, this is a palindrome.
And again, this doesn't always have to be on integers.
We can do it on strings as well.
So we have the word racecar here and we see that we have R and R, A and a, C and C, and then E is
in the middle, which is always going to be there.
So this is also a palindrome.
So for us to implement this.
We'll have a function called palindrome, and we're going to pass in a vector.
We're going to check to see if the vector containing integers is a palindrome or not.
And what we want to do is we want to have the starting index of our vector, which is always going to
be zero.
And then we want to have the ending index, which is going to be length minus one.
Remember, because indexes are always start at zero and then we want to check if the starting index
and the ending index are the same.
And if they are, then we want to reverse inward.
So now we would want to check if A and B are the same.
And then if they are, we know C in the middle is always going to be in the middle because of this being.
The a link the five.
So C will always be in the middle.
So assuming that as the start and end match and then A and B match, then this would also be a palindrome.
So let's take a look at how we can implement this and check to see if we can create some palindromes
ourselves.
So we have our function created.
And we know that we want it to be a vector.
So we'll say we have our array and we'll make it a reference.
That way our function does not absorb
our vector, and then we want to pass in the start and the end, and then we want it to return a boolean.
It helps if I spell you size right.
So in here we have a base case.
We want to see if our start is equal.
Or greater than greater than or equal to our end.
And then if it is return true because and we do this because if our vector is of size one.
Our start would be zero, and then our end would be length minus one, which is also zero.
So we want to make sure that there's more than one element in our array.
Otherwise, we.
Know for a fact that it's a palindrome because there is only one element in there which is always going
to be a palindrome.
So we'll say if the start is equal
to the end, then that is good.
And that means we want to start reversing inward.
So we will keep passing in our array and we will go start plus one to increment our index up one.
And then we will go end minus one to increment D.
Encouragement from the end.
That way we meet somewhere in the middle eventually.
And then if at any point this condition is false, then we want to return false because we found two
elements that do not match when they should match.
Therefore, it is not a palindrome.
So I went ahead and created our test cases here, which we did on.
Her notepad and we expect one, two, three, four to return false.
And then we would expect one, two, three, four, three, two, one to return true.
So if we run this, we see that we have false for the first call, which is correct.
And then we have true for the second call.
So we have a fully functioning palindrome function that is going to look at a sequence of integers inside
of a vector and tell us if it is a palindrome or not.