Erlang记录表达式忽略警告停止应答

Erlang记录表达式忽略警告停止应答,erlang,Erlang,我有以下代码: M =list:sort([X|Y]), list:sort(length) / 2,io:format("The median of the items is", [M]), 但当我试图编译它时,我得到了警告: Warning: the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable) 怎么了?我怎样才能修好它 这是

我有以下代码:

M =list:sort([X|Y]), list:sort(length) / 2,io:format("The median of the items is", [M]),
但当我试图编译它时,我得到了警告:

Warning: the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)
怎么了?我怎样才能修好它

这是我周围代码中的问题,也是我程序中唯一的问题。其他一切都可以

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]),

在您的代码中,
list:sort(length)
将失败,因为length是一个原子,函数正在查找列表,
io:format/2
format字符串缺少用于打印结果的占位符

下面的代码可以工作,至少它可以打印结果,但它总是返回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").
直接在控制台中输入的一些使用示例。您可以看到,当列表包含除数字以外的其他元素时,它将给出一个看似奇怪但技术上正确的答案

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>
1>Answer=fun([])->io:format(“列表中没有项目~n”);
1> (L)当is_list(L)->io:format(“项目的中位数为~p~n”,
1> [列表:第n个((长度(L)+1)div 2,列表:排序(L))];
1> (->io:format(“错误,输入参数不是列表~n”)结束。
#乐趣
2> L1=[6,9,5,7,8]。
[6,9,5,7,8]
3> 答案(L1)。
项目的中位数为7
好啊
4> L2=[4,6,3]。
[4,6,3]
5> 答案(L2)。
项目的中位数为4
好啊
6> L3=[4,6,3,8]。
[4,6,3,8]
7> 答复(L3)。
项目的中位数为4
好啊
8> L4=[你好,5,[1,2]]。
[你好,5[1,2]]
9> 答复(L4)。
项目的中位数是hello
好啊
10> 回答(“测试字符串”)。
这些项目的中位数是114
好啊
11> 回答(测试原子)。
错误,输入参数不是列表
好啊
12> 回答(“”)。
列表中没有项目
好啊
13>

这是我知道如何找到列表中位数的唯一方法…如果其他人知道如何在Erlang中找到中位数,我很想知道!!啊…我明白了…如果列表是即使的,它是如何工作的?如果是,我必须将两个中间数字相加,然后除以2。列表在复杂的io:format调用中排序,我使用了公式
(length+1)div 2
,以在所有长度情况下(奇数或偶数)获得正确的值