erlang - 在运行快速排序之后是否可以输出列表长度? [英] erlang - is it possible to output the list length after having it run a quicksort?

查看:205
本文介绍了erlang - 在运行快速排序之后是否可以输出列表长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Erlang的新手。我已经在随机的数字列表中快速排序(我也只保留了唯一的数字,所以重复的内容不会显示在排序列表中)。它的工作正常,因为输出是给排序的数字没有重复,但我一直试图让它不仅输出列表,而且长度列表也是我正在运行的错误。



length(mod:func)。将给列表的长度在erlang shell中没有问题,但我可以'在快速排序的递归之后,让它工作。我尝试分配变量并执行列表:append 。我只是不知道我在做错什么。



有人可以解释吗?



对不起,我忘了附上下面的代码。这是基本的快速排序。

  -module(list)。 
-export([sort / 0])。
-export([sort / 1])。

sort() - >排序([3,3,3,4,1,2,3,2,6,5,9,11,3,10,5])。

sort([]) - > [];

sort([Pivot | Tail]) - >

sort([X || X < - Tail,X
[Pivot] ++

sort ([X || X - - 尾,X轴])。

当我在shell中运行它时,列表很好。如果我运行长度函数,它给出9是我想要的。

  55> C(列表)。 
{ok,list}
56>清单:排序()。
[1,2,3,4,5,6,9,10,11]
57>长度(列表:排序())。
9

但是我试图让它只是做 list:sort()然后再给出列表和列表的长度。我已经尝试了一堆不同的东西,我已经尝试查找,但似乎找不到如何组合在一个模块中的两个在一起工作。它似乎像长度BIF是相当直接的功能,我只是不使用/正确的方式去。那有意义吗?



我想要这样说:

  55> C(列表)。 
{ok,list}
56>清单:排序()。
[1,2,3,4,5,6,9,10,11]
列表的长度为9


解决方案

您可以使用 io:format / 2 。只需要修改你的排序/ 0函数即可添加一行:

  sort() - > 
Sorted = sort([3,3,3,4,1,2,3,2,6,5,9,11,3,10,5]),
io:format( 〜p〜n列表的长度为〜w〜n,[排序,长度(排序)])。


I'm a newbie to Erlang. I've been running a quick sort on a random list of numbers (i've also had it only keep unique numbers so duplicates do not show up in the sorted list). It works fine in that the output is giving the sorted numbers with no duplicates, but I have been trying to have it output not only the list, but also the length list too which is where I'm running into errors.

The length(mod:func). will give the length of the list no problem in the erlang shell, but I can't get it to work to do it after the recursion of the quick sort. I've tried assigning variables and doing lists:append. I just don't know what I'm doing wrong.

Can anyone explain?

Sorry, I forgot to attach the code below. It's the basic quicksort.

-module(list).
-export([sort/0]).
-export([sort/1]).

sort() -> sort([3,3,3,4,1,2,3,2,6,5,9,11,3,10,5]).

sort([]) -> [];

sort([Pivot|Tail]) ->  

    sort([ X || X <- Tail, X < Pivot]) ++

    [Pivot] ++

    sort([ X || X <- Tail, X > Pivot]).

When I run it in the shell, the list is good. And if I run the length function, it gives 9 which is what I want.

55> c(list).    
{ok,list}
56> list:sort().
[1,2,3,4,5,6,9,10,11]
57> length(list:sort()).
9

But I'm trying to get it to just do list:sort() and then give both the list and the length of the list right after. I've tried a bunch of different things, and I've tried to look it up but can't seem to find how to combine the two in one module to work together. It just seems like the length BIF is pretty straight forward function and I'm just not using/going about it the right way. Does that make sense?

I would like it to say something like:

55> c(list).    
{ok,list}
56> list:sort().
[1,2,3,4,5,6,9,10,11]
The length of the list is 9

解决方案

You can print out the list length by using io:format/2. Just need to modify your sort/0 function to add in a line for it:

 sort() -> 
     Sorted = sort([3,3,3,4,1,2,3,2,6,5,9,11,3,10,5]),
     io:format("~p~nThe length of the list is ~w~n", [Sorted, length(Sorted)]).

这篇关于erlang - 在运行快速排序之后是否可以输出列表长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆