From 7ced55f5c64aedc028d56f40a21fae34a0910315 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 12 Aug 2025 13:58:40 +0000 Subject: [PATCH] [Sync Iteration] elixir/sublist/1 --- solutions/elixir/sublist/1/lib/sublist.ex | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 solutions/elixir/sublist/1/lib/sublist.ex diff --git a/solutions/elixir/sublist/1/lib/sublist.ex b/solutions/elixir/sublist/1/lib/sublist.ex new file mode 100644 index 0000000..e762c37 --- /dev/null +++ b/solutions/elixir/sublist/1/lib/sublist.ex @@ -0,0 +1,20 @@ +defmodule Sublist do + @doc """ + Returns whether the first list is a sublist or a superlist of the second list + and if not whether it is equal or unequal to the second list. + """ + def compare(a, a), do: :equal + def compare(a, b) do + cond do + sublist?(a, b) -> :sublist + sublist?(b, a) -> :superlist + true -> :unequal + end + end + + defp sublist?(a, a), do: true + defp sublist?(a, b) when length(a) > length(b), do: false + defp sublist?(a, [_ | b_tail] = b) do + if List.starts_with?(b, a), do: true, else: sublist?(a, b_tail) + end +end