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

doug.auclair at logicaltypes.com doug.auclair at logicaltypes.com
Wed Mar 15 14:10:27 AEDT 2006


Dear Ralph,

[snip my power_set def]
>I have to say, I find that definition of power_set very hard to
>understand, even given your explanation.  Is it any more efficient than
>
>power_set(Xs) = solutions(subset(Xs)).
>
>subset([],       []      ).
>subset([X | Xs], [X | Ys]) :- subset(Xs, Ys).
>subset([_ | Xs], Ys      ) :- subset(Xs, Ys).
>
>It doesn't seem to be any longer.

Not only is mine less efficient, but yours is prettier.  May
I procure this one for my set_utils module?

[EX2]
>> '..'(CoutList, X, Y) :- unfold(psi(A, A > Y), i, "`+1", X, CountList).
>
>versus
>	X .. Y = ( if X =< Y then [X | (X + 1) .. Y] else [] ).
>
>Again, I find the unfold version quite hard to understand.

But I think the equivalent sections example I gave is just as clear:
>> ... unfold((>Y), id, (+1), X, CountList).

(I changed the 'i' combinator to the equivalent id/1 function here).

Also, in this case, as in most cases, Mercury has the upper hand
over Prolog, as it directly permits functional programming, which
your (also very nice) example exploits.  My caveat was that it was
'..'/2 implemented for Prolog...

[snip EX3 unfolding int to list of bits]
>versus
>	map(func(Num, I) = (X /\ (1 << I)) >> I, 0..31)

Oo! Clever! However, I prefer following the logic of my sections
example:

>> unfold((=< 0), (/\ 1), (>> 1), Num, Bits)

as it tends to make my head spin about less so (I always got that
feeling when I was coding an inject:into: in Smalltalk), but that
may be because I am used to the unfold programming style ...

Sincerely,
Doug Auclair

--------------------------------------------------------------------------
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