Skip to content

Instantly share code, notes, and snippets.

@blippy
Created November 16, 2015 20:13
Show Gist options
  • Save blippy/8354a69f3f349a2b9de3 to your computer and use it in GitHub Desktop.
Save blippy/8354a69f3f349a2b9de3 to your computer and use it in GitHub Desktop.
A list that is automatically stored in order
module OrdList (empty, fromList, insert, toList, OrdList) where
import Data.List as L (span)
data OrdList a = OrdList [a] deriving (Show)
empty :: OrdList a
empty = OrdList []
insert :: (Ord a) => a -> OrdList a -> OrdList a
insert x ol =
let (before, after) = L.span (\el -> el <= x) $ toList ol
in OrdList $ before ++ [x] ++ after
fromList :: Ord a => [a] -> OrdList a
fromList xs =
if null xs then empty else insert (head xs) (fromList $ tail xs)
toList :: OrdList t -> [t]
toList xs =
let OrdList xs' = xs in xs'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment