Created
November 16, 2015 20:13
-
-
Save blippy/8354a69f3f349a2b9de3 to your computer and use it in GitHub Desktop.
A list that is automatically stored in order
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
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