Skip to content

Commit

Permalink
Trees implemented. There is not monad version yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Diaz committed Apr 14, 2012
1 parent d16697f commit 3b274f2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions HaTeX.cabal
Expand Up @@ -69,6 +69,9 @@ Library
Text.LaTeX.Packages.Color.Monad
Text.LaTeX.Packages.Graphicx
Text.LaTeX.Packages.Graphicx.Monad
-- Trees
Text.LaTeX.Packages.Trees
Text.LaTeX.Packages.Trees.Qtree
Extensions: GeneralizedNewtypeDeriving
, OverloadedStrings
, CPP
4 changes: 2 additions & 2 deletions Text/LaTeX/Base.hs
Expand Up @@ -43,5 +43,5 @@ instance Num LaTeX where
negate = (TeXEmpty -)
fromInteger = TeXRaw . fromString . show
-- Non-defined methods
abs _ = "Cannot use \"abs\" Num method with a LaTeX value."
signum _ = "Cannot use \"signum\" Num method with a LaTeX value."
abs _ = error "Cannot use \"abs\" Num method with a LaTeX value."
signum _ = error "Cannot use \"signum\" Num method with a LaTeX value."
10 changes: 10 additions & 0 deletions Text/LaTeX/Packages/Trees.hs
@@ -0,0 +1,10 @@

module Text.LaTeX.Packages.Trees (
-- * Tree
Tree (..)
) where

-- | Tree datatype.
data Tree a =
Leaf a -- ^ Leafs are non-empty.
| Node (Maybe a) [Tree a] -- ^ Node values are optional.
31 changes: 31 additions & 0 deletions Text/LaTeX/Packages/Trees/Qtree.hs
@@ -0,0 +1,31 @@

{-# LANGUAGE OverloadedStrings #-}

module Text.LaTeX.Packages.Trees.Qtree (
-- * Tree to LaTeX rendering
tree
, rendertree
) where

import Text.LaTeX.Base
import Text.LaTeX.Base.Syntax
import Text.LaTeX.Packages.Trees
--
import Data.Monoid
import Data.List (intersperse)

tree_ :: (a -> LaTeX) -> Tree a -> LaTeX
tree_ f (Leaf x) = braces $ f x
tree_ f (Node mx ts) =
mconcat [ "["
, maybe mempty (("." <>) . braces . f) mx
, " "
, mconcat $ intersperse " " $ fmap (tree_ f) ts
, " ]"
]

tree :: (a -> LaTeX) -> Tree a -> LaTeX
tree f t = TeXCommS "Tree" <> " " <> tree_ f t

rendertree :: Render a => Tree a -> LaTeX
rendertree = tree rendertex

0 comments on commit 3b274f2

Please sign in to comment.