I wrote this simple function to append an element to the end of a list recursively:
--takes arguments x and lst and appends x to the end of list
append1::a->[a]->[a]
append1 x [] = x:[]
append1 x (h:t) = h:append1 x t
It works fine. To practice writing tail recursive functions (which I was not aware of until a few days ago), I tried writing it the following way.
--tail recursion
appendt::a->[a]->[a]->[a]
appendt x [] acc = x:acc
appendt x (h:t) acc = appendt x t (h:acc)
It outputs the list I want in reverse. Why this happens I can sort of grasp but it is still hurting my head.
- Why exactly does this happen? Is a general thing that happens when iterating over lists like this?
- is there any way it can be changed (while still using tail recursion/not using ++) to output the list in the correct order?
Note: This is just for practice