{-# LANGUAGE NoMonomorphismRestriction #-} module Main where import Data.Monoid import Control.Applicative import Control.Monad.Writer.Strict import System data BTree = Leaf | Node BTree BTree inc = tell (Sum 1) leaf = inc >> return Leaf node a b = inc >> return (Node a b) make 0 = leaf make depth = do t1 <- make (depth - 1) t2 <- make (depth - 1) node t1 t2 main = do depth <- depthArgs <$> getArgs let counter = getSum . execWriter . make $ depth print counter where depthArgs :: [String] -> Int depthArgs [] = 10 depthArgs [x] = read x {- $ ghc --make -O2 -no-recomp Foo.hs [1 of 1] Compiling Main ( Foo.hs, Foo.o ) Linking Foo ... $ time ./Foo 23 16777215 real 0m0.005s user 0m0.004s sys 0m0.000s -}