Skip to content

Instantly share code, notes, and snippets.

@aergonaut
Last active December 14, 2015 06:49
Show Gist options
  • Save aergonaut/5045898 to your computer and use it in GitHub Desktop.
Save aergonaut/5045898 to your computer and use it in GitHub Desktop.
Reverse an array. Recursive implementation, tail call modulo cons
def reverse(ary)
return ary unless ary.length > 1
reverse(ary[0...ary.length-1]).unshift(ary[-1])
end
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