I decided to dive into the world of functional programming recently, and was introduced to the Haskell language. I started researching how language works, and soon I got the key concepts. So I started working with lists, and decided to reimplement some existing functions, just for practice purposes.
With this, I decided to make my version of the function reverse
, which I called revert
.
Her concept is simple. It receives a list, and returns it inverted. Its implementation is as follows:
revert :: [a] -> [a]
revert [] = []
revert a = revert (tail a) ++ [head a]
It worked, as you can see in the image below:
However,Idecidedtodoanothertest,gettingtheresultofthefunctioninthesamevariablethatIpassedperparameter,asshownbelow:
It performs the function normally, but when I query the value of the variable x
, it seems to enter some type of loop, and you must press Ctrl+C
to cancel.
Detail, it happens only if I receive the same variable as I passed by parameter. If I had done it:
let y = revert x
would have worked, I've tested.
Why does this happen? Is it some quirk I did not get related to some concept of functional programming? I researched but found nothing. Maybe I have not found the correct terms to use in the search ...