Skip to content

Instantly share code, notes, and snippets.

@abhin4v
Last active March 14, 2018 05:36
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 abhin4v/fb214f9be1c81bf2f52d6e15653a1c9b to your computer and use it in GitHub Desktop.
Save abhin4v/fb214f9be1c81bf2f52d6e15653a1c9b to your computer and use it in GitHub Desktop.
Simple on-file number sort in haskell
module Main where
import Data.List (sort)
import Data.List.Split (chunksOf)
import System.IO.Temp (writeSystemTempFile)
readI :: String -> Int
readI = read
merge :: (Ord a) => [a] -> [a] -> [a]
merge [] [] = []
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = if x < y then x : merge xs (y:ys) else y : merge (x:xs) ys
main :: IO ()
main =
getContents
>>= return . map (unlines . map show . sort) . chunksOf 10000 . map readI . lines
>>= mapM (writeSystemTempFile "hsort")
>>= traverse readFile
>>= return . unlines . map show . foldr1 merge . map (map readI . lines)
>>= putStr
-- Usage: cat numbers | fsort > numbers_sorted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment