Erlang - hull design

I am new to Erlang and I have tried some Erlang constructs. My program should behave like this:

if x == 42:
    print "Hi"
else:
    print "Hello"

Here is my code in Erlang

-module(tested).
-export([main/0]).

main() ->
  {ok, X} = io:fread("","~d"),
  case X == 42 of
    true -> io:fwrite("Hi\n");
    false -> io:fwrite("Hello\n")
  end.

Thanks in advance for your help.

+5
source share
1 answer

Use {ok, [X]} = io:fread("","~d")(brackets around X).

freadreturns the list as the second element of the tuple (which makes sense if you are reading more than one token), so you need to get the element from the list before you can compare it with 42.

Note that instead of matching patterns by the result ==, you can simply match the pattern by X itself, that is:

case X of
  42 -> io:fwrite("Hi\n");
  _ -> io:fwrite("Hello\n")
end.
+4
source

All Articles