[mercury-users] Can mercury apply tail call optimization for this piece of code?

Peter Wang novalazy at gmail.com
Wed Mar 30 09:40:32 AEDT 2011


On 2011-03-29, Vladimir Gubarkov <xonixx at gmail.com> wrote:
> Hi,
> 
> I'm experimenting with some optimization options. I know, that mercury
> supports goal-reodering for optimization, so I thought, that it could
> optimize next code to tail-recursive, but seems It's not.
...
> 
> :- pred replaceEvenElementList(list(char), list(char)).
> :- mode replaceEvenElementList(in, out) is det.
> 
> replaceEvenElementList([A,E|T1],[A,E1|T2]):- replaceEvenElementList(T1,T2),
> E1 = (E='x'->'y'; E).

--optimise-constructor-last-call can do it if you move the construction
directly after the recursive call.

replaceEvenElementList([], []).
replaceEvenElementList([A], [A]).
replaceEvenElementList([A, B0 | L0], L) :-
    B = (B0 = 'x' -> 'y' ; B0),
    replaceEvenElementList(L0, L1),
    L = [A, B | L1].

Peter
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list