KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
So, just for fun, I've been playing with a CountedList type in Haskell, using Peano numbers and smart constructors . Type-safe head and tail just seem really cool to me. And I think I've reached the limit of what I know how to do {-# LANGUAGE EmptyDataDecls #-} module CountedList ( Zero, Succ, CountedList, toList, ofList, empty, cons, uncons, head, tail, fmap, map, foldl, foldr, filter ) where import qualified List (foldr, foldl, filter) import Prelude hiding (map, head, foldl, foldr, tail, filter) data Zero data Succ n data CountedList n a = CL [a] toList :: CountedList n a -> [a] toList (CL as) = as ofList :: [a] -> CountedList n a ofList [] = empty ofList (a:as) = cons a $ ofList as empty :: CountedList Zero a empty = CL [] cons :: a -> CountedList n a -> CountedList (Succ n) a cons a = CL . (a:) . toList uncons :: CountedList (Succ n) a -> (a, CountedList n a) uncons (CL (a:as)) = (a, CL as) head :: CountedList (Succ n) a -> a head = fst . uncons tail :: CountedList (Succ n) a -> CountedList n a tail = snd . uncons instance Functor (CountedList n) where fmap f = CL . fmap f . toList map :: (a -> b) -> CountedList n a -> CountedList n b map = fmap foldl :: (a -> b -> a) -> a -> CountedList n b -> a foldl f a = List.foldl f a . toList foldr :: (a -> b -> b) -> b -> CountedList n a -> b foldr f b = List.foldr f b . toList filter :: (a -> Bool) -> CountedList n a -> CountedList m a filter p = ofList . List.filter p . toList (sorry for any transcription errors - the machine I originally wrote this on w/ my Haskell compiler is currently down). Most of what I've done compiles w/o an issue, but I run into issues with ofList and filter . I think I understand why - when I say
Tags (comma-separated)
Save Edits
Cancel