Hi!
Here's a variadic version:
(defn qsort
([] [])
([x & xs] (concat (apply qsort (filter #(< % x) xs))
(cons x (apply qsort (filter #(>= % x) xs))))))
user> (qsort 1234 56 789 0)
(0 56 789 1234)
Kind regards,
achim
Am 20.10.2008 um 17:16 schrieb Stuart Halloway:
>
> 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
>
> >
--
http://rauschabstand.twoday.net
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---