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