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

Ralph Becket rafe at cs.mu.OZ.AU
Tue Mar 14 10:41:02 AEDT 2006


doug.auclair at logicaltypes.com, Monday, 13 March 2006:
> 
> So, I was wondering if something could be added to the standard library
> that would treat n-ary functions as unary functions, multiply curried
> (a la Haskell), and, on the listy end have fold also accept a curried
> unary function that is equivalent to a binary function (call it something
> like cfoldl for 'combinatory'foldleft).

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

> I see the standard approach for 
> Mercury is to create an n-ary lambda term, but it runs counter to the 
> compositional style of functional programming that I follow (after all, 
> why explicitly write a new lambda term when there's an equivalent 
> application?).

Because it's shorter and clearer :-)

	func(X, Y) = X^2 + Y

vs.

	compose((+), converse((^), 2))

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

> On the other side of fold is unfold.  Mercury supplies the series/3 function,
> but when I tried to use it, I was stymied.  The main issue is that X0, etc,
> may need to be transformed while being unfolded.  I see that Mercury follows
> Haskell's unfoldr here, but I was thinking something more like Dylan's
> unfold (http://www.opendylan.org/gdref/gdlibs.html -- sorry, no direct link
> 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 [] ).

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