所以我的第一个问题得到了回答,这是有道理的。它能够在排序后输出列表长度,但最初我要求将 io:format 函数用于排序/0。但我的下一个跟进是如何将它与 sort/1 一起使用?我已经能够解决它,但是它是在递归时给出的,所以我得到了多行并且不正确。我的问题是如何执行 io:format 一旦完成快速排序(另请注意,我也希望列表没有重复)所以我只得到长度的一行而不是我得到的多行以下?
这是我拥有和正在得到的:
-module(list).
-export([sort/1]).
sort([]) -> [];
sort([First|Rest]) ->
io:format("~nThe length of the list is ~w~n", [length([First]++Rest)])),
sort([ X || X <- Rest, X < First]) ++
[First] ++
sort([ X || X <- Rest, X > First]).
和输出:
56> list:sort([2,2,2,3,3,3,1,1,8,6]).
The length of the list is 10
The length of the list is 2
The length of the list is 5
The length of the list is 2
The length of the list is 1
[1,2,3,6,8]
所以没有重复的排序列表是正确的,但是我如何在其中设置 io:format 函数来显示这样的内容?
56> list:sort([2,2,2,3,3,3,1,1,8,6]).
[1,2,3,6,8]
The length of the list is 5