Tail recursion modulo cons

James Hague <>
Wed Nov 20 23:43:33 CET 2002


This is a compiler optimization that effectively makes functions like the
following tail recursive:

map(F, [H|T]) ->
    [F(H)|map(F, T)];
map(_, []) -> [].

What's nice about this is that you can avoid the unintiuitive "accumulator +
big reverse at the end" technique if you want a function to execute in
constant space (and it's easier to explain as well).  As D.H. Warren
introduced this, I'm assuming it's fairly common in Prolog compilers.  It's
implemented in the compiler for Mercury (also a logic language).  Has anyone
ever looked into this for Erlang?

http://burks.brighton.ac.uk/burks/foldoc/12/115.htm



More information about the erlang-questions mailing list