Exercism - Elixir - Hello World

Exercism.IO “Hello World”

Hello World is the introductory exercism challenge for most languages. Exercism gives us a test-case and a partially implemented module with the necessary API interfaces to run the test-case.

# hello_world_test.exs
defmodule HelloWorldTest do
  use ExUnit.Case, async: true

  test "says hello with no name" do
    assert HelloWorld.hello() == "Hello, World!"
  end

  @tag :pending
  test "says hello sample name" do
    assert HelloWorld.hello("Alice") == "Hello, Alice!"
  end

  @tag :pending
  test "says hello other sample name" do
    assert HelloWorld.hello("Bob") == "Hello, Bob!"
  end

end

We must implement the HelloWorld.hello/1 and HelloWorld.hello/2 functions to allow this test-case to run. You may be asking yourself what are these /1 and /2 symbols: this was something totally new to me when I started studying Erlang, it is called function’s arity, the number of arguments a function will receive.

For this exercise, we are given the following partially implemented module:

# hello_world.exs
defmodule HelloWorld do

  # Elixir counts the number of arguments as part of the function name.
  # For instance,

    def hello() do
    
    end

  # would be a completely different function from

    #def hello(name) do
    #end

  # Can you find a way to make all the tests pass with just one function?
  # Hint: look into argument defaults here:
  # http://elixir-lang.org/getting-started/modules.html#default-arguments

  def hello(name) do
  
  end
end

Trivial, sure, there are a lot you can learn from this exercism:

  1. How to run the exercism client., as this was the first Exercism track I played;
  2. How to run Elixir tests, as described on README.md: elixir hello_world_test.exs;
  3. How to enable/disable a test skipping
  4. A perfect place to use String interpolation.

Solving the problem

For the first test, we must implement HelloWorld.hello/1.

    (...)
    def hello() do
    
    end
    (...)

Basically we make this function return the “Hello, World!” string in this function.

    (...)
    def hello() do
      "Hello, World!"
    end
    (...)

Now you can run elixir hello_world_test.exs and you will have passed the first test.

Excluding tags: [:pending]


HelloWorldTest
* test says hello with no name (0.00ms)
* test says hello sample name (skipped)
* test says hello other sample name (skipped)


Finished in 0.02 seconds (0.02s on load, 0.00s on tests)
3 tests, 0 failures, 2 skipped

Randomized with seed 123456

The output is easy to understand. We have three tests, no failures and two skipped. To move forward with the next test, we must untag the :pending mark. Exercism challenges are made to start easy and slowly increase the complexity by the tests ordering, the first test will come untagged and is usually extremely trivial, the next ones will introduce new challenges which makes the exercises fun (obviously, hello_world.exs is not a real challenge, but a introduction).

You can implement HelloWorld.hello/2 and untag the two other tests (by removing @tag :pending), I used String interpolation, I liked the solution, it was nice.

  def hello(name) do 
	"Hello, #{name}!"
  end

Comments

comments powered by Disqus