Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func sumRecursively(numbers: [Int], _ acc:Int = 0) -> Int {
if let head = numbers.first {
let tail = Array(dropFirst(numbers))
return sumRecursively(tail, head + acc)
} else {
return acc
}
}
let myNumbers = [1,2,3,4,5]
sumRecursively(myNumbers) // 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.