Can someone explain me very briefly how the code in the reply of autrijus (It now works. :)) operates?

I'll give it a shot :)

1: multi sub quicksort ( ) { () } 2: multi sub quicksort ( *$x, *@xs ) { 3: my @pre = @xs.grep{ $_ < $x }; 4: my @post = @xs.grep{ $_ >= $x }; 5: (@pre.quicksort, $x, @post.quicksort); 6: } 7: (1, 5, 2, 4, 3).quicksort.say;

I'll assume you're familiar with the quicksort algorithm already, and the way it uses pivots. The first line states how we sort an empty list. Which is simply to return the empty list. This serves as the case for breaking recursion and avoiding infinite looping.

Line two defines a head and a tail, where the head $x is the first element in the list ('1' in the case of the call at line 7), while the tail @xs contains the remaining elements. Line 3 declares a list which contains all the elements from the tail that have a value smaller than $x.

@pre = @xs.grep{ $_ < $x } applies the built-in function grep to the tail. grep returns only the elements in @xs that are smaller than $x and puts them into @pre. This list is still unordered, but we now know that all the values in @pre are smaller than the pivot $x. Similarily for @post, but here we have all the bigger elements.

The magic kicks in at line 5, where we assemble the parts into a new list with the pivot in the middle. Remember though, that @pre and @post are as yet unsorted, so we apply quicksort to them recursively before we return the concatenated list.

Line 7 applies quicksort to a list, and then prints it to screen with the say. Say, I presume, is something similar to Haskell's show and Java's toString().

Clear as ink :D

pernod
--
Mischief. Mayhem. Soap.


In reply to Re: Pugs now works - but how? by pernod
in thread Hello Perl 6. Running pugs on Windows by pernod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.