Ltree inverse crossing function using high order functions
I think this is a continuation-passing solution (simple parser) for writing build:

build :: [(a,Int)] -&gt; LTree a
build xs = parseAt 0 xs fst

-- The first Int is the depth of the "root" of parseAt
parseAt :: Int -&gt; [(a,Int)] -&gt; ( (LTree a, [(a,Int)]) -&gt; r ) -&gt; r
parseAt r [] k = error "input too short"
parseAt r xs@((a,n):rest) k =
  case compare r n of
    GT -&gt; error "depth too small"
    EQ -&gt; k (Leaf a,rest)
    LT -&gt; parseAt (succ r) xs (\ (left,rest1) -&gt;
            parseAt (succ r) rest1 (\ (right,rest2) -&gt;
              k (Fork left right, rest2)))