Skip to content

Instantly share code, notes, and snippets.

@ivarru
Created May 1, 2011 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivarru/950508 to your computer and use it in GitHub Desktop.
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
{-# 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