I'm trying to write a function that reverses a list. The function is recursive.
I know javascript does not have TCO, but i wanted to experiment with this anyway:
reverse = function(list) {
if (list.length < 2) { return list }
fk = fork(list);
return reverse(fk.tail).concat([fk.head])
}
the fork function splits a list to a head and a tail:
fork = function(list) {return {head: list[0], tail: list.slice(1)}}
When I call reverse() with the list [1,2,3,4,5], I get this result:
reverse([1,2,3,4,5]) // [5,4,4,4,4]
Not sure what I'm doing wrong here. The expected result is [5,4,3,2,1].
Please help.
Array.reverse