-
-
Save belyaev-mikhail/0614eddc08c7783e544523b6d6f092cc to your computer and use it in GitHub Desktop.
3_2
This file contains 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
data ReverseList a = RNil | RCons (ReverseList a) a | |
showAsList :: (Show a) => ReverseList a -> String | |
showAsList RNil = "" | |
showAsList (RCons RNil h) = show h | |
showAsList (RCons tl hl) = (showAsList tl) ++ ", " ++ (show hl) | |
toList :: ReverseList a -> [a] | |
toList RNil = [] | |
toList (RCons tl hl) = hl:(toList tl) | |
fromList :: [a] -> ReverseList a | |
fromList [] = RNil | |
fromList (h:t) = RCons (fromList t) h | |
instance (Eq a) => Eq (ReverseList a) where | |
(==) RNil RNil = True | |
(==) RNil _ = False | |
(==) _ RNil = False | |
(==) (RCons tl hl) (RCons tr hr) = hl == hr && tl == tr | |
instance (Ord a) => Ord (ReverseList a) where | |
(<=) RNil RNil = True | |
(<=) RNil _ = True | |
(<=) _ RNil = False | |
(<=) (RCons tl hl) (RCons tr hr) = if (hl <= hr) then True | |
else tl <= tr | |
instance (Show a) => Show (ReverseList a) where | |
show RNil = "[]" | |
show list = "[" ++ (showAsList list) ++ "]" | |
instance Monoid (ReverseList a) where | |
mempty = RNil | |
mappend RNil list = list | |
mappend list RNil = list | |
mappend list (RCons tl hl) = RCons (mappend list tl) hl | |
instance Functor ReverseList where | |
fmap f RNil = RNil | |
fmap f (RCons tl hl) = RCons (fmap f tl) (f hl) | |
test :: String -> String | |
test str = show $ fmap (\h -> h + 1) (RCons (RCons (RNil) 2) 3) | |
main = interact test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment