Functions Everywhere

Chris recently got addicted to functions and he presents his best implementation of functions to you:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function list_of_size(n) {
    function term(x) {
        return x * (x + 1);
    }
    function list_helper(count) {
        if (count > n) {
            return function(f) {
                return -1;
            };
        } else {
            return function(f) {
                return f(term(count), list_helper(count + 1));
            };
        }
    }

    return list_helper(1);
}

function head(f) {
    return f(function (p, q) { return p; });
}

function tail(f) {
    return f(function (p, q) { return q; });
}

function sum(list) {
    if (head(list) === -1) {
        return 0;
    } else {
        return head(list) + sum(tail(list));
    }
}

What is the value of sum(list_of_size(3))?

×

Problem Loading...

Note Loading...

Set Loading...