Created
July 7, 2026 10:08
-
-
Save nickserv/6465ce4e00cdc572983507a2008ffb32 to your computer and use it in GitHub Desktop.
QuickSort in Haskell
This file contains hidden or 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
| -- code from https://play.haskell.org/ with my own commentary | |
| -- import one function from one module | |
| import Data.List (partition) | |
| -- main is the entry point for I/O side effects and lets you prefer to have imperative programming with the "do" operator | |
| main :: IO () | |
| main = do | |
| let unsorted = [10,9..1] | |
| -- $ means group the rest of the line as function ars together (like a left paren) | |
| putStrLn $ show $ quicksort unsorted | |
| -- you declare a type signature once, with generic type constraints between :: and => | |
| quicksort :: Ord a => [a] -> [a] | |
| -- you can declare implementations for different signatures separately, which lets us declare the recursive alogorithm's base case without an explicit conditional | |
| quicksort [] = [] | |
| -- x is the first list item and xs gets the rest (similar to car and cdr in LISP, or first and rest in modern languages) | |
| quicksort (x:xs) = let (lesser, greater) = partition (<= x) xs | |
| in quicksort lesser ++ [x] ++ quicksort greater |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment