Think about what you want your base case to be. Do you want just elem
, or do you want a list with the single item elem
? There is a difference. If the want the latter, you will need to fix your base case in the code.
In other words, do you want (appendElem '() 42)
to return 42
, or (42)
? Think carefully about the answer to that question, then think about what the consequence of each choice is.
By the way, while you can implement appendElem
as a toy, you'll soon realise that that function has O(n) runtime. So do not build lists using this approach! The standard way to build a list is to cons
items to it, then reverse
the final resulting list.