The tree of function calls made by a naive Fibonacci implementation.
import Diagrams.Backend.SVG.CmdLine
{-# LANGUAGE NoMonomorphismRestriction #-}
import Diagrams.Prelude
We make use of a tree layout module from the diagrams-contrib
package:
import Diagrams.TwoD.Layout.Tree
Generate the tree of naive Fibonacci calls.
fibCalls :: Integer -> BTree Integer
0 = leaf 0
fibCalls 1 = leaf 1
fibCalls = BNode n (fibCalls (n-1)) (fibCalls (n-2)) fibCalls n
Lay out the tree and render it by providing a function to render nodes and a function to render edges.
Just t = uniqueXLayout 2 2 (fibCalls 5)
= pad 1.1 . centerXY
example $ renderTree
-> (text ("fib " ++ show n)
(\n <> roundedRect 3 1.3 0.3 # fc gold)
)~~) t
(`atop` square 1 # scaleY 12 # translateY (-5)
# scaleX 34
# lw none # fc whitesmoke
= mainWith (example :: Diagram B) main