---
# System prepended metadata

title: CS1101S Reflection 03C Higher Order Functions and Scope  97108856

---

# CS1101S Reflection 03C Higher Order Functions and Scope  97108856


```javascript=

// 1 Use the substitution model to “manually” run the following program. 
const z = 1;
function f(g) { 
    const z = 3; 
    return g(z);
}
f(y => y + z);

//f(y => y + z)
//f(y => y + 1)
//(y => y + 1)(z)
//(y => y + 1)(3)
//4

//
//Write a function my sum that computes the following sum, for n ≥ 1, without higher-order functions:
// 1×2+2×3+⋯+n×(n+1)

function my_sum(n) {
    return n === 0 
           ? 0
           : n * (n + 1) + my_sum(n - 1);
}

// the function first evaluates whether n = 0, and then if it is the function returns my_sum(0) as 0 which means the function ends at 1*2 as its last term

// the function is recursive and gives rise to a recursive process

// 3
/**
 * Does the function my sum as defined in Question 2 give rise to a recursive process or an
iterative process? What is the order of growth in time and in space, using Θ notation? **/

Recursive process. Order of growth in time and space are both Θ(n).


// 4. We can also define my sum in terms of the higher-order function sum. Complete the decla- ration of my sum below. You cannot change the definition of sum; you may only call it with appropriate arguments.

function my_sum(n) { return sum(<T1>, <T2>, <T3>, <T4>); }
*/

function sum(term, a, next, b) {
    return (a > b) ? 0
                   : term(a) + sum(term, next(a), next, b);
}

function my_sum(n) { 
    return sum(x => x * (x + 1), 
               1, 
               x => x + 1, 
               n); 
}

// 5. Write an iterative versions of sum.
function sum(term, a, next, b){
    return sum_iter(term, a, next, b, 0);
}

function sum_iter(term, a, next, b, total){
    return (a > b)
    ? total
    : sum_iter(term, next(a), next, b, total + term(a));
}
    

// BREAKOUT ROOM 6
// 6 AB. Given the following source program:
// Quy Duc, Guanzhou, Gregory Wong
// A What names are declared by this program?
// functions f, h & g, constants x
// (b) Which declaration does each name occurrence refer to?
// functions f, h & g, constants x
const x = 5;
function f(g) {
    const x = 3;
    return x => x + g(x); 
}
function g(f, y) {
    const h = (y, f) => y(f);
    return h(f, y);
}


// BREAKOUT ROOM 7
//   6 C. Given the following source program:
// USE THE SUBSTITUTION MODEL
// Bao Bin, Sheryl-Lynn, Wen Cong
// (C) What is the value of 
(f(x => 2 * x))(4)?
(f(x => 2 * x))(4);
(x => x + (x => 2 * x))(4)
(4 + (2 * 4))
(4 + 8)
12
const x = 5;
function f(g) {
    const x = 3;
    return x => x + g(x); 
}
function g(f, y) {
    const h = (y, f) => y(f);
    return h(f, y);
}

f(x => 2 * x))(4);
(x => x + (x => 2 * x))(4);
(4 + (x => 2 * x));



// DONE BY AIDEN
// 6D. Given the following source program:
// USE THE SUBSTITUTION MODEL
// (D) What is the value of g(y => y + 2, x)?
const x = 5;
function f(g) {
    const x = 3;
    return x => x + g(x); 
}
function g(f, y) {
    const h = (y, f) => y(f);
    return h(f, y);
}

g(y => y + 2, x);
```