Created
June 23, 2016 03:35
-
-
Save tomcha/6b6a4965226d0338aef19aaf472812d2 to your computer and use it in GitHub Desktop.
recursion16.rb
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
| 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