[m-dev.] HO List args: fold and unfold

Ralph Becket rafe at cs.mu.OZ.AU
Wed Mar 15 14:43:40 AEDT 2006


doug.auclair at logicaltypes.com, Tuesday, 14 March 2006:
> Not only is mine less efficient, but yours is prettier.  May
> I procure this one for my set_utils module?

Be my guest, but this one is more efficient:

power_set([])       = [[]].
power_set([X | Xs]) = 
    foldr(func(S, SS) = [S, [X | S] | SS], power_set(Xs), []).

This version does the same thing, but allocates fewer cons cells:

power_set([])       = [[]].
power_set([X | Xs]) = PXXs :-
    PXs  = power_set(Xs),
    PXXs = foldr(func(S, SS) = [[X | S] | SS], PXs, PXs).

-- Ralph
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list