Last active
November 27, 2017 23:13
-
-
Save seanwestfall/015b01cc3099ad542e53b5cb67e429d6 to your computer and use it in GitHub Desktop.
QuickSort implemented in PureScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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