import Data.List data Pair a = P a a data MultiTuplu a = Null | Single { get :: a } | Pair { fs :: a, sd :: a } | Triple { t1 :: a, t2 :: a, t3 :: a } deriving (Show, Read) data NestedList a = Atom a | List [NestedList a] deriving Read fromAtom (Atom x) = x fromAtom _ = undefined nl = List [Atom 1, Atom 2, List [Atom 3, Atom 4, Atom 5], Atom 6] instance Show a => Show (NestedList a) where show (Atom x) = "<" ++ show x ++ ">" show (List l) = "{" ++ concat (intersperse ", " $ map show l) ++ "}" instance Show a => Show (Pair a) where show (P x y) = "<" ++ show x ++ ", " ++ show y ++ ">" l2 = List [List [List [Atom 4, Atom 5, Atom 6], Atom 7, Atom 8], List [Atom 10, Atom 11], Atom 12] data Tree a = Nil | Leaf { val :: a } | Node { val :: a, chd :: [Tree a] } --deriving (Show, Ord) myTree = Node "radăcină" [ Node "copil1" [ Leaf "frunză 1", Leaf "frunză 2"], Leaf "frunză 3"] -- arbore binar infinit care în toate nodurile conține numărul 1 treeOnes = Node 1 [treeOnes, treeOnes] -- arbore binar infinit care la BFS dă lista de numere treeB = build 1 where build n = Node n (map build [2*n, 2*n+1]) instance Show a => Show (Tree a) where show tree = pt 0 tree where pt level tree = case tree of Nil -> space level ++ "- \n" Leaf val -> space level ++ show val ++ "\n" Node val children -> space level ++ show val ++ "\n" ++ if all isNil children then "" else concatMap (pt $ level + 1) children space sp = [' ' | _ <- [1..sp * 2]] isNil Nil = True isNil _ = False limit 0 _ = Nil limit level t = case t of Node val children -> Node val $ map (limit (level-1)) children _ -> t preord Nil = [] preord (Leaf v) = [v] preord (Node v children) = v : concatMap preord children class Invertible a where invert :: a -> a invert = id instance Eq a => Eq (Tree a) where Nil == Nil = True Node x _ == Node y _ = x == y instance Invertible a => Invertible [a] where invert = reverse . map invert instance Invertible (Tree a) where invert (Node v chd) = Node v $ invert chd invert x = x class Container t where -- t trebuie să fie unqr contents :: t a -> [a] instance Container [] where contents = id instance Container Tree where contents = preord instance Functor Tree where fmap _ Nil = Nil fmap f (Leaf x) = Leaf $ f x fmap f (Node x chd) = Node (f x) $ map (fmap f) chd -- ghci> fmap ("prefix"++) myTree -- ghci> fmap ("prefix"++) myTree