• + 0 comments

    I decided I didn't want to use the Enum library at all and implemented my Elixir solution using pure naive comprehensions. One of the interesting things about recursion is that the list is represented backwards in memory immediately after make_list is run, but since filter walks the list again and places the last element in the first position, everything comes back out in the correct order once it is time to print :D

    defmodule NaiveList do
      def print([]), do:
        nil
      def print([value | rest]) do
        IO.puts(value)
        print(rest)
      end
    
      def filter(limit, list), do:
        filter(limit, list, [])
      def filter(_limit, [], output), do:
        output
      def filter(limit, [value | rest], output) when value < limit, do:
        filter(limit, rest, [value | output])
      def filter(limit, [_value| rest], output), do:
        filter(limit, rest, output)
    
      def make_list(), do:
        make_list(IO.gets(""), [])
      defp make_list(""<>number, list), do:
        make_list(IO.gets(""), [(number |> String.trim |> String.to_integer) | list])
      defp make_list(:eof, list), do:
        list
    end
    
    n = IO.gets("") |> String.trim |> String.to_integer
    list = NaiveList.make_list()
    NaiveList.filter(n, list) |> NaiveList.print