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

; Clojure, destructuring pivot and vals
(defn quicksort-1 [[pivot & vals]]
   (if pivot
     (let [smaller (partial filter #(< % pivot))
          bigger (partial filter #(>= % pivot))]
       (lazy-cat (quicksort-1 (smaller vals)) [pivot] (quicksort-1  
(bigger vals))))
     nil))

; Clojure again, using multimethod to separate base and recur
(defmulti quicksort-2 first)
(defmethod quicksort-2 nil [_] nil)
(defmethod quicksort-2 :default [[pivot & vals]]
   (let [smaller (partial filter #(< % pivot))
        bigger (partial filter #(>= % pivot))]
     (lazy-cat (quicksort-2 (smaller vals)) [pivot] (quicksort-2  
(bigger vals)))))

I am confident that the Clojure could be prettier, but not sure how.  
Suggestions?

Cheers,
Stuart

--~--~---------~--~----~------------~-------~--~----~
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