Skip to content

Instantly share code, notes, and snippets.

@jmsevold
Created February 6, 2016 05:04
Show Gist options
  • Save jmsevold/fb49165d743b15793e80 to your computer and use it in GitHub Desktop.
Save jmsevold/fb49165d743b15793e80 to your computer and use it in GitHub Desktop.
reverse a list in elixir
defmodule ListReverse do
def reverse(list) do
reverse(list,[])
end
defp reverse([h|t],result) do
reversed = [h] ++ result
reverse(t,reversed)
end
defp reverse([],result) do
result
end
end
@czogori
Copy link

czogori commented Jul 22, 2017

more compact version ;)

defmodule ListReverse do
  def reverse([]), do: []
  def reverse([h|t]), do: reverse(t) ++ [h]
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment