billc
4/3/2017 - 8:28 PM

Assignment 6: List sorting

Assignment 6: List sorting

defmodule Assignment6 do
  def main(_args) do
    IO.gets("Enter a name: ")
    |> cleanize
    |> input
    |> Enum.sort         # I cheated and use a merge sort after all input collected
    |> IO.inspect
  end

  @doc """
  An input name of "end" completes user input and returns an initialized blank input
  """
  def input("end") do
    []
  end

  @doc """
  Recursively inputs a name and adds to a list
  """
  def input(name) do
    n = IO.gets "Enter a name: "
    [name | n |> cleanize |> input]
  end

  @doc """
  Removes any trailing new line characters or spaces, then lower cases the name
  """
  def cleanize(s) do
    s
    |> String.trim_trailing
    |> String.downcase
  end

end