Hi Steve,

Thanks! I like quicksort-4. It all fixes a problem that bugged me in  
all the other examples, which is that "bigger" is a lie. The real  
semantic is "not smaller", which quicksort-4 captures perfectly.

I will have to get used to thinking of "remove" as the opposite of  
"filter." The English connotations of the former are too imperative  
for me, but I don't know a better word for it.

Stuart

> On Oct 20, 2008, at 11:16 AM, Stuart Halloway wrote:
>
>> Hi all,
>>
>> I seem to recall Rich saying "I like the destructuring part of  
>> pattern
>> matching." In my efforts to appreciate that statement, I am playing
>> around with porting simple Haskell examples to Clojure, trying to use
>> destructuring (and multimethods) where the Haskell does pattern  
>> matches.
>>
>> For example:
>>
>> -- Haskell
>> qsort []     = []
>> qsort (x:xs) = qsort smaller ++ [x] ++ qsort bigger
>>     where smaller = filter (<x)  xs
>>           bigger = filter (>=x) xs
>>
>> [...]
>
>> I am confident that the Clojure could be prettier, but not sure how.
>> Suggestions?
>
> Here are a couple of ideas:
>
> The first is a more literal translation of the Haskell code with  
> 'when providing the nil case:
>
> (defn quicksort-3 [[x & xs]]
>   (when x
>     (let [smaller (filter #(< % x) xs)
>           bigger (filter #(>= % x) xs)]
>       (lazy-cat (quicksort-3 smaller)
>                 [x]
>                 (quicksort-3 bigger)))))
>
> The second uses the filter/remove complementary pair so there's only  
> one predicate:
>
> (defn quicksort-4 [[x & xs]]
>   (when x
>     (let [smaller #(< % x)]
>       (lazy-cat (quicksort-4 (filter smaller xs))
>                 [x]
>                 (quicksort-4 (remove smaller xs))))))
>
> --Steve
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to