fun bruijn(le, lamlist) =
let fun depth(s, lst) = if lst=[] then raise NotClosed
else if List.hd(lst)=s then 0
else 1+depth(s,List.tl(lst))
in
case le of VAR(s) => I(depth(s, lamlist))
| LAM(s, e) => L(bruijn(e, s::lamlist))
| APP(e1, e2) => A(bruijn(e1,lamlist), bruijn(e2,lamlist))
end
fun bruijnize le = bruijn(le, [])
:roll: :D :shock: :)