Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created June 23, 2011 14:40
Show Gist options
  • Save wyattdanger/1042656 to your computer and use it in GitHub Desktop.
Save wyattdanger/1042656 to your computer and use it in GitHub Desktop.
attempting to port a function from SICP lecture video.
var tree = [ 1, [2, 3], 4, 5];
function first(arr) {
return arr[0];
}
function rest(arr) {
return arr.slice(1);
}
function add(a,b) {
return a + b;
}
function sumFringe(tree) {
if ( ! tree.length ) {
return ( typeof tree == "number") ? tree : 0;
} else if ( tree.length == 1 ) {
return first(tree);
} else {
return add( sumFringe(first(tree)), sumFringe(rest(tree)));
}
}
console.log(sumFringe(tree));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment