{-# LANGUAGE BangPatterns, MagicHash #-} module M (len) where import GHC.Exts data Tree a = Tip | Bin a !(Tree a) !(Tree a) foldrTree :: (a -> b -> b) -> b -> Tree a -> b foldrTree f z0 t = go t z0 where go Tip z = z go (Bin x l r) z = go l (f x (go r z)) len :: Tree a -> Int len t = foldrTree (\_ k -> oneShot (\ !acc -> k (acc + 1))) id t 0 -- lenAlt :: Tree a -> Int -- lenAlt t = foldrTree (\_ k -> oneShot (\acc# -> k (acc# +# 1#))) I# t 0#