[mercury-users] accumulator introduction

Michael Day mcda at students.cs.mu.oz.au
Fri Jun 23 13:11:05 AEST 2000


Is there any way for Mercury to automatically convert this predicate to
use an accumulator, or is it condemned to overflow the detstack forever?

sum(Input, Output) :-
    ( if Input = 0 then
        Output = Input
    else
        sum(Input-1, Output0),
        Output = Output0 + Input
    ).

Adding an accumulator manually to make it tail recursive gives performance
quite close to a C loop:

for (i = 0; i <= Input; ++i) sum += i;

which is rather nice. But it would be nicer if it added the accumulator
itself. Is this possible?

Michael
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list