Skip to content

Instantly share code, notes, and snippets.

@nickserv
Created July 7, 2026 10:08
Show Gist options
  • Select an option

  • Save nickserv/6465ce4e00cdc572983507a2008ffb32 to your computer and use it in GitHub Desktop.

Select an option

Save nickserv/6465ce4e00cdc572983507a2008ffb32 to your computer and use it in GitHub Desktop.
QuickSort in Haskell
-- 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