Kapitel 3: Typen und Typklassen hugs Lay ghc -c BinTree.lhs Lay.lhs > module Lay > where > import BinTree hiding (splitAt) Text mit Einrückung. < data Text > text :: String -> Text -- ohne |'\n'| > nl :: Text > indent :: Int -> Text -> Text > (<>) :: Text -> Text -> Text > > render :: Text -> String > data Text = Text String -- ohne |'\n'| > | Newline > | Indent Int Text > | Text :<> Text > > text = Text > nl = Newline > indent = Indent > (<>) = (:<>) > render' :: Int -> Text -> String -> String > render' i (Text s) x = s ++ x > render' i Newline x = '\n' : replicate i ' ' ++ x > render' i (Indent j d) x = render' (i + j) d x > render' i (d1 :<> d2) x = render' i d1 (render' i d2 x) > render t = render' 0 t "" > class (Show a) => Lay a where > lay :: a -> Text > > lay a = text (show a) > > instance Lay Int > instance Lay Integer > instance (Lay a) => Lay (Tree a) where > lay Empty = text "Empty" > lay (Node l a r) = indent 4 (text "Node" <> nl <> > lay l <> nl <> > lay a <> nl <> > lay r) putStrLn $ render $ lay (Node (Node Empty 4711 Empty) 12 Empty :: Tree Int) putStrLn $ render $ lay (prune 4 (big 1)) > instance (Lay a) => Lay [a] where > lay [] = text "[]" > lay (a : x) = text "[" <> lay a <> layl x > where layl [] = text "]" > layl (b : y) = nl <> text "," <> lay b <> layl y putStrLn $ render $ lay [1 .. 99 :: Int] putStrLn $ render $ lay [prune i (big 1) | i <- [0 .. 4] ]