Skip to content

Instantly share code, notes, and snippets.

@ChristopheBelpaire
Created December 12, 2014 12:17
Show Gist options
  • Save ChristopheBelpaire/53612bd0bd21077f719f to your computer and use it in GitHub Desktop.
Save ChristopheBelpaire/53612bd0bd21077f719f to your computer and use it in GitHub Desktop.
My 'sublist' solution for exercism.io
defmodule Sublist do
defp count(l) do
count(l,0)
end
defp count([], size) do
size
end
defp count([_|tail], size) do
count tail, size+1
end
def compare(a,a) do
:equal
end
def compare(a, b) do
if count(a) > count(b) do
is_inside(a, b, :superlist)
else
is_inside(b, a, :sublist)
end
end
def is_inside(_, [], kind) do
kind
end
def is_inside([], _, _) do
:unequal
end
def is_inside([head_a| tail_a], b, kind) do
if starts_with([head_a| tail_a], b) do
kind
else
is_inside(tail_a, b, kind)
end
end
def starts_with([], []) do
true
end
def starts_with([], _) do
false
end
def starts_with(_, []) do
true
end
def starts_with([head_a| tail_a], [head_a| tail_b]) do
starts_with(tail_a, tail_b)
end
def starts_with([_head_a| _tail_a], [_head_b| _tail_b]) do
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment