Created
May 1, 2011 13:46
-
-
Save ivarru/950508 to your computer and use it in GitHub Desktop.
Another Haskell solution to the task given at Oslo Coding Dojo, April 2011
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
{-# LANGUAGE TypeFamilies #-} | |
{-# OPTIONS_GHC | |
-XFlexibleInstances -XOverlappingInstances -XUndecidableInstances | |
-XFlexibleContexts -XScopedTypeVariables #-} | |
module Tree where | |
class Tree t where | |
type Contents t | |
fold :: (Contents t -> [a] -> a) -> t -> a | |
instance (Tree t, Show (Contents t)) => Show t where | |
show = unlines . fold f | |
where f x = (show x :) . map ("\t" ++) . concat | |
data ConcreteTree a = Node a [ConcreteTree a] | |
instance Tree (ConcreteTree a) where | |
type Contents (ConcreteTree a) = a | |
fold f (Node x rest) = f x $ map (fold f) rest | |
testTree = | |
Node "parent" [ | |
Node "child1" [], | |
Node "child2" [ | |
Node "grandchild1" [], | |
Node "grandchild2" []]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment