Skip to content

Instantly share code, notes, and snippets.

@seanwestfall
Last active November 27, 2017 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanwestfall/015b01cc3099ad542e53b5cb67e429d6 to your computer and use it in GitHub Desktop.
Save seanwestfall/015b01cc3099ad542e53b5cb67e429d6 to your computer and use it in GitHub Desktop.
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