From a52453ed54c7a4f119536db5997c4826a4eccd4b Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Thu, 16 Sep 2021 17:00:38 +0200 Subject: [PATCH] Implement a simple version of flatten --- maps/myenum.exs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/maps/myenum.exs b/maps/myenum.exs index 2cbd775..e6955cb 100644 --- a/maps/myenum.exs +++ b/maps/myenum.exs @@ -45,6 +45,23 @@ defmodule MyEnum do end end + # I gave up on these two. # split # take end + +defmodule MyList do + def flatten([]) do + [] + end + + def flatten([head|tail]) do + # this is basically the recursion + flatten(head) ++ flatten(tail) + end + + def flatten(head) do + # if there's only on element, we make a list out of it, so that we can concat them + [head] + end +end