This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1A | |
function compute_e(lst) { | |
function helper_acc(xs, n, current){ | |
if(is_empty_list(xs)) { | |
return current; | |
} else { | |
if (n % 2 === 0){ | |
return helper_acc(tail(xs), n + 1, current + head(xs)); //if index is even (so number is at odd i), just add it to the sum | |
} else { | |
return helper_acc(tail(xs), n + 1, current + head(xs) * 3); //if index is odd (so number is at even i), multiply by 3 and then add to sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Obtain sum of array using for-loops | |
/* Thought process: this is the same as our sum for lists but in an imperative style of programming. So the idea is the same as recursion | |
Array indexes start at 0 and end at n - 1, where n is the number of elements in the array. Therefore, we start at 0 and end at n-1, where we move 1 element each time | |
*/ | |
function sumArray(arr) { | |
let sum = 0; //initialize counter at 0 because we haven't added anything yet (we usually start counting with 0 in CS). | |
NewerOlder