Newbie Question about io:format

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Newbie Question about io:format

Hakim Fajardo
First, thank you because this is a great resource. 

Second, I started writing basic programs every day to improve my Erlang coding. I wrote a simple program that calculates the average of a list of numbers. But I keep getting this error about io:format.


prices([]) ->
[],
io:format("List is empty ~n");

%% determine number of items in the list to calculate average%%

prices(L) when is_list(L) ->
Size = length(L),
Sum1 = lists:sum(L),
Av1 = Sum1 / Size,
io:format("The average equals ~n", Av1).

Then..

Eshell V8.2  (abort with ^G)
1> L = [1,2,2,2,3,3,4].
[1,2,2,2,3,3,4]

2> c(standev).
{ok,standev}

3> standev:prices(L).
** exception error: bad argument
     in function  io:format/3
        called as io:format(<0.50.0>,"The average equals ~n",2.4285714285714284)


Why am I getting '**exception error:'?

Thank you, I hope this isn't too newbie a question.




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Newbie Question about io:format

Dmytro Lytovchenko
Because Erlang has no functions with variable amount of arguments (like for example in printf C/C++), args to io:format are passed as a list. So correct way to call it is:

io:format("Bla ~p~n", [Av1]).

Where ~p is to print any value, also there are more ways to format a value, consult the documentation page at http://erlang.org/doc/man/io.html#fwrite-1

2017-02-21 10:15 GMT+01:00 Hakim Fajardo <[hidden email]>:
First, thank you because this is a great resource. 

Second, I started writing basic programs every day to improve my Erlang coding. I wrote a simple program that calculates the average of a list of numbers. But I keep getting this error about io:format.


prices([]) ->
[],
io:format("List is empty ~n");

%% determine number of items in the list to calculate average%%

prices(L) when is_list(L) ->
Size = length(L),
Sum1 = lists:sum(L),
Av1 = Sum1 / Size,
io:format("The average equals ~n", Av1).

Then..

Eshell V8.2  (abort with ^G)
1> L = [1,2,2,2,3,3,4].
[1,2,2,2,3,3,4]

2> c(standev).
{ok,standev}

3> standev:prices(L).
** exception error: bad argument
     in function  io:format/3
        called as io:format(<0.50.0>,"The average equals ~n",2.4285714285714284)


Why am I getting '**exception error:'?

Thank you, I hope this isn't too newbie a question.




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Newbie Question about io:format

Richard A. O'Keefe-2
In reply to this post by Hakim Fajardo
On 21/02/17 10:15 PM, Hakim Fajardo wrote:
(simplified):

prices([]) ->
     io:format("List is empty~n");
prices(L) when is_list(L) ->
     io:format("The average equals ~n", lists:sum(L)/length(L)).
               ^^^^^^^^^^^^^^^^^^^^^^^

% man io
        format(Format, Data) -> ok
                  Data = [term()]

The second argument of io:format/2 should be a list.

What part of the format says to expect a number?
What part of the format says how to print a number?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Newbie Question about io:format

Hakim Fajardo
In reply to this post by Dmytro Lytovchenko
Thank you,

I understand now.

On Tue, Feb 21, 2017 at 4:42 AM, Dmytro Lytovchenko <[hidden email]> wrote:
Because Erlang has no functions with variable amount of arguments (like for example in printf C/C++), args to io:format are passed as a list. So correct way to call it is:

io:format("Bla ~p~n", [Av1]).

Where ~p is to print any value, also there are more ways to format a value, consult the documentation page at http://erlang.org/doc/man/io.html#fwrite-1

2017-02-21 10:15 GMT+01:00 Hakim Fajardo <[hidden email]>:
First, thank you because this is a great resource. 

Second, I started writing basic programs every day to improve my Erlang coding. I wrote a simple program that calculates the average of a list of numbers. But I keep getting this error about io:format.


prices([]) ->
[],
io:format("List is empty ~n");

%% determine number of items in the list to calculate average%%

prices(L) when is_list(L) ->
Size = length(L),
Sum1 = lists:sum(L),
Av1 = Sum1 / Size,
io:format("The average equals ~n", Av1).

Then..

Eshell V8.2  (abort with ^G)
1> L = [1,2,2,2,3,3,4].
[1,2,2,2,3,3,4]

2> c(standev).
{ok,standev}

3> standev:prices(L).
** exception error: bad argument
     in function  io:format/3
        called as io:format(<0.50.0>,"The average equals ~n",2.4285714285714284)


Why am I getting '**exception error:'?

Thank you, I hope this isn't too newbie a question.




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Newbie Question about io:format

Hakim Fajardo
In reply to this post by Richard A. O'Keefe-2
Thank you! I see now. 

On Tue, Feb 21, 2017 at 7:17 PM, Richard A. O'Keefe <[hidden email]> wrote:
On 21/02/17 10:15 PM, Hakim Fajardo wrote:
(simplified):

prices([]) ->
    io:format("List is empty~n");
prices(L) when is_list(L) ->
    io:format("The average equals ~n", lists:sum(L)/length(L)).
              ^^^^^^^^^^^^^^^^^^^^^^^

% man io
       format(Format, Data) -> ok
                 Data = [term()]

The second argument of io:format/2 should be a list.

What part of the format says to expect a number?
What part of the format says how to print a number?


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...