Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created June 23, 2016 03:34
Show Gist options
  • Select an option

  • Save tomcha/22a07d0e30d37fb1ae6b1d3e8b1244ec to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/22a07d0e30d37fb1ae6b1d3e8b1244ec to your computer and use it in GitHub Desktop.
recursion15.rb
list = [1, 3, 5]
#pro = Proc.new{|i| 0..i }
pro = Proc.new{|i|
res ||= []
(0..i).each do |j|
res << j
end
res
}
def collect(list, pro)
@ans ||= []
if list.size == 0
@ans
else
i = list.shift
@ans << pro.call(i)
collect(list, pro)
end
end
p collect(list, pro)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment