function arrayToList(arr) {
    var list = null;
    for (var i = arr.length - 1; i >= 0; --i)
        list = { value: arr[i], rest: list };
    return list;
}

function listToArray(list) {
    var arr = [];
    var myList = list;
    while (myList) {            // More elegant: for (var node = list; node; node = node.rest)
        arr.push(myList.value);
        myList = myList.rest;
    }
    return arr;
}

function prepend(num, list) {
    return { value: num, rest: list };
}

function nth(list, num) {
    var node = list;
    for (var i = 0; i < num; ++i) {
        if (! node.rest)
            return undefined;
        node = node.rest;
    }
    return node.value;
}

function nthRecurse(list, num) {
    if (! list)
        return undefined;
    if (num === 0)
        return list.value;
    return nthRecurse(list.rest, num - 1);
}

function arrayToList(array) {
    let list = null;
    for (let i = array.length - 1; i >= 0; --i) {
        list = {value: array[i], rest: list};
    }
    return list;
}

function listToArray(list) {
    let array = [];
    for (let node = list; node; node = node.rest) {
        array.push(node.value);
    }
    return array;
}

function prepend(element, list) {
    return arrayToList([element, ...listToArray(list)]);
}

function nth(list, n) {
    if (n === 0) return list.value;
    else return list.rest ? nth(list.rest, n-1) : null;
}

Add Chapter 04, exercise 3
Add Chapter 04, exercise 3
