user3102798
user3102798

Reputation: 1

Erlang record expression ignored warning stopping answer

I have following code:

M =list:sort([X|Y]), list:sort(length) / 2,io:format("The median of the items is", [M]),

But I get warning when I try to compile it:

Warning: the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)

What's wrong? How can i fix it?

This is it within my surrounding code and its the only problem with my program. Everything else works.

answer([ ]) -> io:format(" There are no items in the list");

answer([X|Y]) ->
   M =list:sort([X|Y]), list:sort(length) / 2,io:format("The median of the items is", [M]),

Upvotes: 0

Views: 464

Answers (1)

Pascal
Pascal

Reputation: 14042

in your code, list:sort(length) will fail as length is an atom and the function is looking for a list, and the io:format/2 format string is missing a place holder to print the result.

the following code works, al least it prints the results, but it always return ok.

answer([ ]) -> io:format("There are no items in the list~n");
answer(L) when is_list(L) -> io:format("The median of the items is ~p~n",
                                [lists:nth((length(L)+1) div 2,lists:sort(L))]);
answer(_) -> io:format("error,the input parameter is not a list~n").

some example of use directly entered in the console. You can see that it will give an answer which can seem weird, while technically correct, when the list contains other element than numbers.

1> Answer = fun([ ]) -> io:format("There are no items in the list~n");             
1> (L) when is_list(L) -> io:format("The median of the items is ~p~n",             
1>                                 [lists:nth((length(L)+1) div 2,lists:sort(L))]);
1> (_) -> io:format("error,the input parameter is not a list~n") end.              
#Fun<erl_eval.6.80484245>
2> L1 = [6,9,5,7,8].
[6,9,5,7,8]
3> Answer(L1).
The median of the items is 7
ok
4> L2 = [4,6,3].
[4,6,3]
5> Answer(L2).  
The median of the items is 4
ok
6> L3 = [4,6,3,8].
[4,6,3,8]
7> Answer(L3).    
The median of the items is 4
ok
8> L4 = [hello, 5,[1,2]].   
[hello,5,[1,2]]
9> Answer(L4).           
The median of the items is hello
ok
10> Answer("test_string").
The median of the items is 114
ok
11> Answer(test_atom).    
error,the input parameter is not a list
ok
12> Answer("").       
There are no items in the list
ok
13>

Upvotes: 2

Related Questions