Last active
August 29, 2015 14:04
-
-
Save rnapier/e757aed05afc1811a6cc to your computer and use it in GitHub Desktop.
Tail-recursive reverse
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
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