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