Skip to content

Instantly share code, notes, and snippets.

@rebeccaskinner
Created May 13, 2024 14:47
Show Gist options
  • Save rebeccaskinner/0bb9d85901eaca132116ab89c254f6c4 to your computer and use it in GitHub Desktop.
Save rebeccaskinner/0bb9d85901eaca132116ab89c254f6c4 to your computer and use it in GitHub Desktop.
Compare lengths of finite and infinite lists
data Length = forall x. Length { unLength :: [x] }
instance Eq Length where
(==) (Length []) (Length []) = True
(==) (Length (_:xs)) (Length (_:ys)) = Length xs == Length ys
(==) _ _ = False
instance Ord Length where
compare (Length []) (Length []) = EQ
compare (Length (_:_)) (Length []) = GT
compare (Length []) (Length (_:_)) = LT
compare (Length (_:xs)) (Length (_:ys)) = compare (Length xs) (Length ys)
toLength :: Length -> Int
toLength (Length xs) = length xs
length' :: [x] -> Length
length' = Length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment