I'm curious to understand how this works, I have my doubts:
function arrayToList(array) {
let list = null;
for (let i = array.length - 1; i >= 0; i--) {
list = {value: array[i], rest: list};
}
return list;
What use is this?
function listToArray(list) {
let array = [];
for (let node = list; node; node = node.rest) {
array.push(node.value);
}
return array;
}
and this:
function prepend(value, list) {
return {value, rest: list};
}
and this one too:
function nth(list, n) {
if (!list) return undefined;
else if (n == 0) return list.value;
else return nth(list.rest, n - 1);
}
I have a notion of what it does, but I'm confused.
"value" will have the first value of the argument
"rest" is an object with the properties "value" and "rest, where "value" is the next value of the argument and "rest" an object with the same properties described but with the "rest" with the values of the next iteration .
Output:
the second returns a new array with the values of the argument
Returns an object with properties "value" and "rest", where "value" is the first argument and "rest" is the second argument
{ value: 'test value', rest: [ 1, 2, 3 ] }