Created
February 21, 2015 18:56
-
-
Save JFFail/7c502d901228e9d355fd to your computer and use it in GitHub Desktop.
Simple example for myself of a recursive function to compute the sum of a list.
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
#!/usr/bin/ruby | |
def sums(list) | |
if list.length > 1 | |
return list.pop + sums(list) | |
else | |
return list.pop | |
end | |
end | |
#Make a list. | |
my_list = [3, 2, 4, 1] | |
the_sum = sums(my_list) | |
puts "The sum of the list is: #{the_sum}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment