Writing "Hello World" N Times

In order to learn functional programming and about Concurrency this year I picked Erlang.

I also want to keep solving some problems so that the syntax becomes very familier to me and I found Hackerrank has functional programming domain which lets you solve problems.

I will be posting whatever I try and new things I learned. This may be useful to others who are new to Erlang or Functional Programming.

Problem?

Print "Hello World" N times. The input portion will be handled automatically. You need to write a function with the recommended method signature.

This sounds extremely simple to me using list comprehensions and it is, but part I was not sure was how to read from standard input and write to standard output in Erlang.

After some google searches, I found exact same question on StackOverflow.

I found out that I need to know about fread and fwrite. That was it.

My Solution

-module(hr).
-author("harith").

-export([main/0]).

main() ->
  {ok, [N]} = io:fread("", "~d"),
  hello(N).

hello(N) when N >= 0, N =< 50 ->
  [io:format("Hello World~n") || _I <- lists:seq(1,N)].

Let me know if you have a better way of doing this.

comments powered by Disqus