Skip to content

Instantly share code, notes, and snippets.

@marcelofabri
Created January 2, 2015 22:33
Show Gist options
  • Save marcelofabri/4b41adb87a09ce86ff40 to your computer and use it in GitHub Desktop.
Save marcelofabri/4b41adb87a09ce86ff40 to your computer and use it in GitHub Desktop.
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