iMikou96
iMikou96

Reputation: 90

How to call a printing function with more than two arguments on ERLANG?

So I've wrote this program that takes 1 String Argument and print it as following:

-module(sayhi).

-export([sayhi/1]).

sayhi({greeting, Greeting}) -> io:fwrite(Greeting).

then I call the function as following (from terminal).

c(sayhi).

ok
sayhi:sayhi({greeting, "HELLO!\n"}).
HELLO!
ok

Until now everything is good.

But When I try to implement 2 arguments, I get error: *** argument 1: wrong number of arguments

Here is my Code:

-module(sayhi).

-export([sayhi/2]).
    
sayhi({greeting, Greeting}, {name, Name}) -> io:fwrite(Greeting, Name).

When I call my function:

sayhi:sayhi({greeting, "Hola "}, {name, "Sam"}).

The program Runs successfully but does not give me the output needed. does the problem come from my statement of calling the function?

And what if I had 3, or even 10 arguments?

Upvotes: 2

Views: 216

Answers (2)

7stud
7stud

Reputation: 48659

You can also take advantage of what Erlang calls an iolist, which is a list containing certain types, which can be strings, and for an iolist Erlang will output the list as one string. For example:

-module(a).
-compile(export_all).
    
sayhi({greeting, Words})->
    io:format(Words)

In the shell:

6> c(a).                                   
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

    {ok,a}

7> a:sayhi({greeting, ["Hello ", "Jim!"]}).
Hello Jim!ok

io:format() returns ok, and that's what you see at the end of the string. That's not the greatest solution because you have to add spaces to your words (notice the space in "Hello "). A better solution would be to automatically add spaces between words, then add a newline at the end so that the ok is on a separate line:

-module(a).
-compile(export_all).

sayhi({greeting, Words})->
    WordsWithSpaces = add_spaces(Words, []),
    io:format(WordsWithSpaces).


add_spaces([Word | []], Acc) ->
    lists:reverse([$\n, Word | Acc]);
add_spaces([Word | Words], Acc) ->
    add_spaces(Words, [" ", Word | Acc]).

In the shell:

18> c(a).
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

{ok,a}

19> a:sayhi({greeting, ["Hello", "Jim!", "Now", "goodbye!"]}).
Hello Jim! Now goodbye!
ok

Upvotes: 2

Agus
Agus

Reputation: 689

Erlang has a comprehensive documentation on all of its built-in functions, such as io:fwrite/1, io:fwrite/2, io:fwrite/3 (can be found here).

If you want to use the function with 1 argument, then you can call it like this:

io:fwrite(Greeting ++ Name). %% '++' is nothing but appending strings
io:fwrite(Greeting ++ Name ++ NextParam1 ++ NextParam2). %% You can then expand it as needed

When using 2 arguments, i.e. write(Format, Data), then:

io:fwrite("~s~s", [Greeting, Name]).
io:fwrite("~s~s~s~s", [Greeting, Name, NextParam1, NextParam2]). %% You can also expand as needed

Upvotes: 2

Related Questions