Skip to content

Instantly share code, notes, and snippets.

@wende
Last active May 16, 2016 20:53
Show Gist options
  • Save wende/b2d8dc898b5d0e7d22f9063e8f06b6c6 to your computer and use it in GitHub Desktop.
Save wende/b2d8dc898b5d0e7d22f9063e8f06b6c6 to your computer and use it in GitHub Desktop.
def common_root(xs, root), do: common_root(xs, root, String.length(root))
def common_root([h | _] = xs, root \\ "", length \\ 0) do
newroot = elem(String.split_at(h, length + 1), 0)
if Enum.all?(xs, &String.starts_with?(&1, newroot)) do
common_root(xs, newroot, length + 1)
else
root
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment