Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2015 10:21
Show Gist options
  • Save anonymous/3c0f5694ca059d7609c6 to your computer and use it in GitHub Desktop.
Save anonymous/3c0f5694ca059d7609c6 to your computer and use it in GitHub Desktop.
recursion
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