Created
January 26, 2015 10:21
-
-
Save anonymous/3c0f5694ca059d7609c6 to your computer and use it in GitHub Desktop.
recursion
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
def rest(ary) | |
_,*r = ary | |
r | |
end | |
# Strategy1 (compute while going down the stack) | |
def add1(col) | |
def add1helper(col,accu) | |
if col.empty? | |
accu | |
else | |
add1helper(rest(col),accu + col.first) | |
end | |
end | |
add1helper(col,0) | |
end | |
# Strategy 2 (compute while going up the stack) | |
def add2(col) | |
if col.empty? | |
0 | |
else | |
col.first + add2(rest(col)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment