Last active
January 2, 2016 18:29
-
-
Save leandronsp/8344283 to your computer and use it in GitHub Desktop.
ruby the functional way
This file contains 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
def flist(list) | |
clone = list.dup | |
return [] if list == [] | |
[ clone.shift, clone ] | |
end | |
def len(list, acc = 0) | |
return acc if list == [] | |
_, tail = flist list | |
len(tail, acc + 1) | |
end | |
def sum(list, acc = 0) | |
return acc if list == [] | |
head, tail = flist list | |
sum(tail, acc + head) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment