Published on 28 April 2022 (Updated: 25 January 2023)

Welcome to the Quick Sort in Haskell page! Here, you’ll find the source code for this program as well as a description of how the program works.

Current Solution

moduleMainwhereimportText.ReadimportSystem.EnvironmentimportSystem.Exit(exitWith,ExitCode(ExitFailure))importData.List(intercalate)quickSort::Orda=>[a]->[a]quickSort[]=[]quickSort(x:xs)=letleft=quickSort$filter(<=x)xsright=quickSort$filter(>x)xsinleft++[x]++rightheadMaybe::[a]->MaybeaheadMaybe[]=NothingheadMaybe(x:xs)=Justx-- Converts string in format "1, 2, 3" to a Maybe list of intstringToListMaybe::String->Maybe[Int]stringToListMaybestr=readMaybe$"["++str++"]"::Maybe[Int]-- Ensure that a list contains at least two elementsverifyListLength::Orda=>[a]->Maybe[a]verifyListLength[]=NothingverifyListLength[x]=NothingverifyListLength(x:xs)=Just(x:xs)listToString::[Int]->StringlistToString=intercalate", ".mapshowmain::IO()main=doargs<-getArgsletxs=headMaybeargs>>=stringToListMaybe>>=verifyListLengthcasexsofNothing->doputStrLn"Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\""exitWith$ExitFailure1Justxs->putStrLn$listToString$quickSortxs

Note: The solution shown above is the current solution in the Sample Programs repository as of Mar 26 2019 01:28:05. The solution was first committed on Dec 04 2018 20:35:19. As a result, documentation below may be outdated.