Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
QuickSort implemented in PureScript
module QuickSort where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.List (List(..), (:), filter)
import Data.List as S
import TryPureScript (render, withConsole)
-- | main :: forall e. Eff (console :: CONSOLE | e) Unit
main = render =<< withConsole do
let sortedList = quicksort (S.fromFoldable [10,2,5,3,1,6,7,4,2,3,4,8,9])
logShow sortedList -- (1 : 2 : 2 : 3 : 3 : 4 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : Nil)
quicksort :: forall a. Ord a => List a -> List a
quicksort Nil = Nil
quicksort (x:xs) = (quicksort lesser) `append` (x : quicksort greater)
where
lesser = filter (\n -> n < x) xs
greater = filter (\n -> n >= x) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment