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

doug.auclair at logicaltypes.com doug.auclair at logicaltypes.com
Wed Mar 15 07:21:36 AEDT 2006


Dear Ralph, thanks for your reply.  You wrote:

>I once wrote a fancy currying library thinking it would be useful.
>Neither I nor anyone else ever used it.

Awww!  I would've used it (hope that makes you feel better: you
are honoured and valued; your individuality is unique and
beautiful, ... like a snowflake).

[re: currying]
>Because it's shorter and clearer :-)
>	func(X, Y) = X^2 + Y
>vs.
>	compose((+), converse((^), 2))

That depends (http://www.cotilliongroup.com/man/combinators-man.html#ex-for-ralph):

vs:   ``b+`w* 
which is shorter and clearer than either example, and, the
material point, what I'm aiming at.  (Incidentally, do you
like the name of the link to the example?)

>There is also a performance penalty with interpreting arity n
>functions as unary functions returning arity n functions.

Yes, I know speed is a foundational issue for the Mercury team,
but I'm willing to pay those two microseconds of runtime
for the n minutes or hours saved during development (after all,
I'm not talking just one 'sum-of-squares' coding exercise, we
have modules that use curried functions in every clause of the
predicate for data transformation -- one of these predicates
has ~100 clauses).  Also, this is a pay-as-needed feature.  
Those who do no use cfoldl do not pay the curried function
argument cost.  And, finally, currying can get pretty hairy,
but most of it can be unwound with static analysis, so that
there is actually no runtime cost, and compiler would do
pretty much the same rewriting that it already does for
regular HO arguments.

There.

I'll grant that not many non-Haskellers/MLers use currying
regularly, if at all, and the number of people who use
combinators day-to-day for real stuff is even smaller (I
make number 3 in the world, I think).  So, I'll port this
code from Prolog for my own use.  Should I submit it for
inclusion into extras/?

>> to the function description).  Not having the transformation 
>> function for the current element sometimes renders series/3 
>> useless, so I was wondering if unfold could be added to the
>> list module.

>Is this what you're after:
>:- func unfold(pred(T1, T2, T1)::in(pred(in, out, out) is semidet), T1::in) =
>	(list(T2)::out) is det.
>
>unfold(P, A0) = ( if P(A0, X, A) then [X | unfold(P, A)] else [] ).

Sweet!  So is this function going into module list?

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