Skip to content

Instantly share code, notes, and snippets.

@foliveira
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foliveira/b79e12481ab50ae90941 to your computer and use it in GitHub Desktop.
Save foliveira/b79e12481ab50ae90941 to your computer and use it in GitHub Desktop.
Fun with Elixir
defmodule Fun do
def max([x]), do: x
def max([ head | tail ]), do: Kernel.max(head, max(tail))
end
defmodule Fun do
def slice(x, start, length), do: x
end
defmodule Fun do
def swap([head | tail], _, 1, carry, rest), do: [head] ++ List.delete_at(rest, -1) ++ [carry] ++ tail
def swap([head | tail], 1, second, carry, rest), do: swap(tail, 1, second - 1, carry, rest ++ [hd(tail)])
def swap([head | tail], first, second, _, rest), do: [head] ++ swap(tail, first - 1, second - 1, hd(tail), rest)
def swap(x, first, second), do: swap(x, first, second, hd(x), [])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment