Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created February 21, 2015 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/7c502d901228e9d355fd to your computer and use it in GitHub Desktop.
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.
#!/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