1

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
muehsi
  • 588
  • 3
  • 19
chitown88
  • 27,527
  • 4
  • 30
  • 59

2 Answers2

1

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)]).
Máté
  • 2,294
  • 3
  • 18
  • 25
  • Ok. But won't this give the length of that specific list. Not only was I trying to sort, I was more specifically doin it to only have a list of the unique values. So I'd want the length of the unique value list. so with my example, I'd want a length output of 9. But wont the on above have a length of 15? – chitown88 Mar 26 '17 at 17:01
  • 1
    The result of your quicksort will be assigned to `Sorted` on the first line, hence it will have the value of the desired list and the length of it. – Máté Mar 26 '17 at 17:39
  • 2
    @chitown88, *But wont the on above have a length of 15?*-No. All the answer does is add a variable to the first line of your sort/0 function, and the variable stores the result of your sort/1 function. Then the answer adds a second line to your sort/0 function, which prints out the length of list stored in the Sorted variable...but why couldn't you just try it yourself to verify that the length was 9 and not 15? – 7stud Mar 26 '17 at 17:40
  • @7stud, I did try it right after I added the comment and realized it. I should have checked and tried it out before. I'm still new to all this and learning. But I do appreciate your explanation. I kind of need those explanations to follow the logic and make sense of it in my head. – chitown88 Mar 27 '17 at 12:02
  • Again, thank you. I'm still trying to understand how it works and the only way I'll learn is to ask questions.Even if dumb ??s with obvious answers. So now my follow up, if wanted to run a different list from the shell, let's say I input the following and want out: `56> list:sort(55,66,55,55,44). [44,55,66] The length of the list is 3` how is the code affected? the part that I'm getting stuck is I realize it's doing a recursion and stops at an empty set and can't sort anymore. where does the `io:format` fit in then to say, take **that** result to output the length? – chitown88 Mar 27 '17 at 12:57
  • @matov, How can I print out the list length by using io:format/2 with the sort/1 function? Where does that fit in? I get all kinds of errors, bad argument or sort/1 already defined, etc. – chitown88 Mar 27 '17 at 13:13
  • 1
    @chitown88 If I understand correctly, you'd like to pass in a list as a parameter, and still get the results out, right? In that case, I'd do is rename your `sort/1` to `qsort/1`, as that's the internal function you are using. Then, turn your current `sort/0` into a function that accepts a list as a parameter, and you have what you're looking for. – Máté Mar 27 '17 at 14:29
  • @matov; yes that completely makes sense. I hadn't thought of it in that way. Thanks again! – chitown88 Mar 27 '17 at 15:31
  • @chitown88 happy to help. Feel free to accept my responses as the answer, if they satisfied your question(s). – Máté Mar 27 '17 at 19:03
1
1> S = fun S([]) -> {[],0};                 
1> S([P|T]) ->                              
1> {Small,LSmall} = S([X || X <- T, X < P]),
1> {Big,LBig}= S([X || X <- T, X > P]),     
1> {Small ++ [P] ++ Big, LSmall+LBig+1}     
1> end.                                     
#Fun<erl_eval.30.52032458>
2> S([3,3,3,4,1,2,3,2,6,5,9,11,3,10,5]).
{[1,2,3,4,5,6,9,10,11],9}
3>
Pascal
  • 13,977
  • 2
  • 24
  • 32