Created
June 23, 2016 03:34
-
-
Save tomcha/22a07d0e30d37fb1ae6b1d3e8b1244ec to your computer and use it in GitHub Desktop.
recursion15.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| 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