[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: qsort optimized for brevity

William James

9/26/2015 9:39:00 PM

> It is buggy. Sorry. Here is a better version
>
> (defun qsort (list &aux (x (first list)) (xs (rest list)))
> (when list
> (append (qsort (remove-if (lambda (y) (>= y x)) xs))
> (list x)
> (qsort (remove-if (lambda (y) (< y x)) xs)))))

Gauche Scheme:

(define (qsort xs)
(if (null? xs)
xs
(receive (lt gt) (partition (pa$ > (car xs)) (cdr xs))
`(,@(qsort lt) ,(car xs) ,@(qsort gt)))))


gosh> (qsort '(9 2 0 88 33 22 1983 1066 3))
(0 2 3 9 22 33 88 1066 1983)

--
Africans gang-rape and clitorectomize Finnish girl; government arrests Finn
whom they accuse of complaining:
conservative-headlines.com/2009/03/another-european-awaits-extradition-for-hate-speech
3 Answers

William James

11/1/2015 7:04:00 PM

0

WJ wrote:

> > It is buggy. Sorry. Here is a better version
> >
> > (defun qsort (list &aux (x (first list)) (xs (rest list)))
> > (when list
> > (append (qsort (remove-if (lambda (y) (>= y x)) xs))
> > (list x)
> > (qsort (remove-if (lambda (y) (< y x)) xs)))))
>
> Gauche Scheme:
>
> (define (qsort xs)
> (if (null? xs)
> xs
> (receive (lt gt) (partition (pa$ > (car xs)) (cdr xs))
> `(,@(qsort lt) ,(car xs) ,@(qsort gt)))))
>
>
> gosh> (qsort '(9 2 0 88 33 22 1983 1066 3))
> (0 2 3 9 22 33 88 1066 1983)

It's shorter in MatzLisp (Ruby):

def qsort((f,*r))
f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []
end

Explanation:

def qsort((f,*r))

The array passed to the function is destructured; f receives
the first element, and r receives the rest.

f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []

The
<condition> ? <do-if-true> : <do-if-false>
construct is borrowed from C.

If f is not nil (the array isn't empty), then l is
bound to the numbers less than f, and g receives the numbers
greater than or equal to f (the first number in the array).

The rest should be easy to understand.

qsort [9,2,0,88,33,22,1983,1066,3]
==>[0, 2, 3, 9, 22, 33, 88, 1066, 1983]

--
It is extremely gratifying to ... achieve membership in an elite that is going
to make history.... The only drawback is that the self-appointed shepherds are
likely to find eventually that they were only sheepdogs and herded the flock
toward a destination of which they knew nothing. --- R. P. Oliver

Kaz Kylheku

11/1/2015 10:50:00 PM

0

On 2015-11-01, WJ <w_a_x_man@yahoo.com> wrote:
> WJ wrote:
>
>> > It is buggy. Sorry. Here is a better version
>> >
>> > (defun qsort (list &aux (x (first list)) (xs (rest list)))
>> > (when list
>> > (append (qsort (remove-if (lambda (y) (>= y x)) xs))
>> > (list x)
>> > (qsort (remove-if (lambda (y) (< y x)) xs)))))
>>
>> Gauche Scheme:
>>
>> (define (qsort xs)
>> (if (null? xs)
>> xs
>> (receive (lt gt) (partition (pa$ > (car xs)) (cdr xs))
>> `(,@(qsort lt) ,(car xs) ,@(qsort gt)))))
>>
>>
>> gosh> (qsort '(9 2 0 88 33 22 1983 1066 3))
>> (0 2 3 9 22 33 88 1066 1983)
>
> It's shorter in MatzLisp (Ruby):

Only by raw character count, I'm afraid.

> def qsort((f,*r))
> f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []
> end

Let's count all symbols which denote operators and operands:

(define (qsort xs)
1 2 3

(if (null? xs)
4 5 6

xs
7

(receive (lt gt) (partition (pa$ > (car xs)) (cdr xs))
8 9 10 11 12 13 14 14 16 17

`(,@(qsort lt) ,(car xs) ,@(qsort gt)))))
18 19 20 21 22 23 24 25 26 27


def qsort((f,*r))
1 2 3 4 5

f ? (l,g=r.partition{|x| f > x}; qsort(l) + [ f ] + qsort(g)) : []
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

end

If Ruby's "end" regarded as mere punctuation, they are are of equal symbolic
length, 27 and 27.

> Explanation:

None required for the Scheme, note.

William James

5/9/2016 2:04:00 PM

0

WJ wrote:

> WJ wrote:
>
> > > It is buggy. Sorry. Here is a better version
> > >
> > > (defun qsort (list &aux (x (first list)) (xs (rest list)))
> > > (when list
> > > (append (qsort (remove-if (lambda (y) (>= y x)) xs))
> > > (list x)
> > > (qsort (remove-if (lambda (y) (< y x)) xs)))))
> >
> > Gauche Scheme:
> >
> > (define (qsort xs)
> > (if (null? xs)
> > xs
> > (receive (lt gt) (partition (pa$ > (car xs)) (cdr xs))
> > `(,@(qsort lt) ,(car xs) ,@(qsort gt)))))
> >
> >
> > gosh> (qsort '(9 2 0 88 33 22 1983 1066 3))
> > (0 2 3 9 22 33 88 1066 1983)
>
> It's shorter in MatzLisp (Ruby):
>
> def qsort((f,*r))
> f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []
> end
>
> Explanation:
>
> def qsort((f,*r))
>
> The array passed to the function is destructured; f receives
> the first element, and r receives the rest.
>
> f ? (l,g=r.partition{|x| f>x}; qsort(l)+[f]+qsort(g)) : []
>
> The
> <condition> ? <do-if-true> : <do-if-false>
> construct is borrowed from C.
>
> If f is not nil (the array isn't empty), then l is
> bound to the numbers less than f, and g receives the numbers
> greater than or equal to f (the first number in the array).
>
> The rest should be easy to understand.
>
> qsort [9,2,0,88,33,22,1983,1066,3]
> ==>[0, 2, 3, 9, 22, 33, 88, 1066, 1983]

OCaml:

let rec qsort = function
[] -> []
| x::xs -> let (l,g) = List.partition (fun e -> x>e) xs
in qsort l @ x :: qsort g ;;

--
[I]n 1917, ... they had a provisional government set up after the Czar had
abdicated, and ... [the Jew] Kerensky came in as head of the government....
[H]e ... outlawed "anti-Semitism", and even imposed a death penalty for
"anti-Semitism". http://www.redicecreations.com/radio/2016/03/RIR-...