3
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 was able to get the main concepts. So I started working with lists, and decided to reimplement some existing functions, just for practice purposes.
With that, I decided to make my version of the function reverse
, who I called revert
.
Its concept is simple. It takes a list, and returns it reversed. Its implementation is the following:
revert :: [a] -> [a]
revert [] = []
revert a = revert (tail a) ++ [head a]
It worked, as you can see in the image below:
However, I decided to do another test, receiving the function result in the same variable that I passed by parameter, as shown below:
It performs the function normally, but when I refer to the variable value x
, it seems to enter some kind of loop, and it is necessary to tighten Ctrl+C
to cancel.
Detail, it only happens if I receive in the same variable I passed by parameter. If I had done:
let y = revert x
would have worked, I tested.
Why does this happen? Is it some peculiarity that I didn’t get related to some concept of functional programming? I researched but found nothing. Maybe I haven’t found the right terms to use in the search...
It reminded me of the subject of LP, the suffering rs +1 good question
– Marconi