Created
June 23, 2011 14:40
-
-
Save wyattdanger/1042656 to your computer and use it in GitHub Desktop.
attempting to port a function from SICP lecture video.
This file contains hidden or 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
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