Bubblesort (was: Re: Why aren't we all speaking LISP now?)

Mats Kindahl matkin at iar.se
Fri May 11 12:11:16 EDT 2001


sax at no-host-at-all.no (Stefan Axelsson) writes:

> In article <roy-C59972.07100611052001 at news1.panix.com>,
> Roy Smith  <roy at panix.com> wrote:
[snip]
> I was going to keep quiet since I'm going away for the week and won't
> be here to keep the argument going, but I cannot hold my peace! ;-)
> 
> In Haskell a quick sort is:
> 
> qsort []     = [] 
> qsort (x:xs) = qsort[y | y <- xs, y < x] ++ [x] ++ qsort[y | y <- xs, y >= x]
[snip]
> I'm sure that something equally elegant could be accomplished in
> Python (though I'm too new to Python I'm afraid), it has the
> prerequisites (list comprehension, lists etc), which is why I didn't
> feel too bad about the above Haskell code. It'd be a bit insulting an
> comp.lang.c for example ;-) ;-)

Well, not equally elegant: the closest to your example is

def qsort(list):
  if (len(list) == 0):
    return []
  else:
    (x,xs) = (list[0],list[1:])
    return qsort([y for y in xs if y < x ]) + [x] + qsort([y for y in xs if y >= x])

Pretty close though... :)
 
List comprehension was one of the things I found really neat in
Haskell and missed in all other languages I've used... especially
those having built-in lists.

> The moral: it's not that HLL:s have sorting built in, it's that they
> enable you to express powerful concepts without being mired in
> detail. (Which incidentally, and IMHO, is why students always cry help
> when exposed to functional programing, the easy stuff that lasts a
> whole course on imperative programming is finished off in a few weeks,
> and then the real fun begins. ;-)

Agreed.
 
At the keyboard,
-- 
Mats Kindahl, IAR Systems, Sweden

Any opinions expressed are my own, not my company's.




More information about the Python-list mailing list