Sorry disturbing you,
What about this one?
fun mem l xs = let fun mem l (x :: xs) a = if x = l then (List.rev a) @ xs else mem l xs (x :: a) | mem l [] a = raise (Fail "Not found"); in mem' l xs [] end;
Regards, Andras Pahi
-----Eredeti ?zenet----- Felad?: polyml-bounces@inf.ed.ac.uk [mailto:polyml-bounces@inf.ed.ac.uk]Meghatalmaz? David Matthews K?ldve: Tuesday, October 16, 2007 9:13 PM C?mzett: Lukas Bulwahn M?solatot kap: PolyML mailing list T?rgy: Re: [polyml] Question about possible compiler optimization
Lukas Bulwahn wrote:
I was mistaken that this function is tail recursive. But I was expecting that the function can be translated to the following C code (using no stack allocations):
while (p is not Nil) do if (value of p == l) then p := p->next; else break; od if (p is Nil) then raise Error...
So, this program should not allocate any stack.
That is a different function. It doesn't return the modified list but merely checks that the item is or is not present. The equivalent, tail-recursive function in ML is:
fun test l [] = raise Fail "Not present" | test l (x::xs) = if l = x then () else test l xs;
Poly/ML detects this as tail-recursive and turns it into a loop.
David _______________________________________________ polyml mailing list polyml@inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml