Last active
December 14, 2015 06:49
-
-
Save aergonaut/5045898 to your computer and use it in GitHub Desktop.
Reverse an array. Recursive implementation, tail call modulo cons
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
def reverse(ary) | |
return ary unless ary.length > 1 | |
reverse(ary[0...ary.length-1]).unshift(ary[-1]) | |
end |
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
let rec reverse xs = match xs with | |
| [] -> [] | |
| [x] -> [x] | |
| head::tail -> reverse tail @ [head] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment