Skip to content

Instantly share code, notes, and snippets.

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

  • Save tomcha/6b6a4965226d0338aef19aaf472812d2 to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/6b6a4965226d0338aef19aaf472812d2 to your computer and use it in GitHub Desktop.
recursion16.rb
list = [1, 3, 5]
pro = Proc.new{|i|
arr ||= []
(0..i).each do |j|
arr << j
end
arr
}
def collect(list, pro)
@arr ||= []
if list.size == 0
@arr
else
i = list.shift
# pro.call(i).each do |j|
# @arr << j
# end
@arr += 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