We have seen that the reverse function can be used to check if a string is a palindrome. This function can be used in many other applications. For instance, if you want to read about the history of a country, you will consider the books from the older to the newer. Therefore, it is very useful to sort these books from the older to the newer. However, when all these books are read, you may want to write your book on the contemporary history of your country. In this situation, sorting the books from the newer to the older may be more interesting. Your reverse function can perform this job.
Consider the function reverse implemented with an accumulator:
fun {Reverse L}
fun {ReverseAux L Acc}
case L of nil then Acc
[] H|T then {ReverseAux T H|Acc}
end
end
in
{ReverseAux L nil}
end
The function Reverse reverses a list such that [1 2 3 4] becomes [4 3 2 1].
You are asked to transform Reverse in order to use explicit states (cells). In other words, you will not have to use recursion anymore, but "for loops". Here is an usage of "for loops":
for I in 1..10 do
{Browse I}
end
Or like this:
for E in L do
{Browse E}
end
Do not define auxiliary functions (or procedures) in Reverse.
Your code will be placed in the following framework:
fun {Reverse L}
[YOUR CODE]
end
FROMIMPLICITTOEXPLICIT
fun {Reverse L}
Correct