Skip to content

Instantly share code, notes, and snippets.

@leandronsp
Last active January 2, 2016 18:29
Show Gist options
  • Save leandronsp/8344283 to your computer and use it in GitHub Desktop.
Save leandronsp/8344283 to your computer and use it in GitHub Desktop.
ruby the functional way
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