Skip to content

Instantly share code, notes, and snippets.

@rnapier
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnapier/e757aed05afc1811a6cc to your computer and use it in GitHub Desktop.
Save rnapier/e757aed05afc1811a6cc to your computer and use it in GitHub Desktop.
Tail-recursive reverse
func reverseR<T>(result: [T], curList: Slice<T>) -> [T] {
switch curList.first {
case .None: return result
case .Some(let cur): return reverseR([cur] + result, dropFirst(curList))
}
}
func reverseTailRecursive<T>(ls: [T]) -> [T] {
return reverseR([], Slice(ls))
}
println(reverseTailRecursive([1,2,3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment