tail recursion modulo cons
tail recursion modulo cons
(programming, compiler)f [] = []f (x:xs) = 1 : f xs
is transformed into (pseudo C/Haskell):
f [] = []f l = f' l allocate_cons
f' [] p = { *p = nil;return *p}f' (x:xs) p = { cell = allocate_cons;*p = cell;cell.head = 1;return f' xs &cell.tail}
where allocate_cons returns the address of a new cons cell, *pis the location pointed to by p and &c is the address of c.
[D.H.D. Warren, DAI Research Report 141, University ofEdinburgh 1980].