Higher order programming

Ralph Becket rwab1 at cam.sri.com
Thu Apr 22 02:21:50 AEST 1999


I'm a great advocate of higher-order programming, but I find that it's
not as easy in Mercury as it ought to be, sometimes due to the syntax
involved, but more often because of library predicates not being
provided in `forward only' versions or not taking `function' HO args.

For example, if you want to sum the elements of a list, you end up
doing something like the following:

sum_list(Xs, Sum) :-

	Add = ( pred(X::in, Acc:in, (X + Acc)::out) is det ),

	list__foldl(Add, Xs, 0, Sum).

where what you'd really like to do is just

sum_list(Xs, Sum) :- list__foldl((+), Xs, 0, Sum).

or, even better (if we're in function land)

sum_list(Xs) = list__foldl((+), Xs, 0).

The problem, of course, is that the compiler doesn't like HO arguments
that have multiple modes.  I'm sure this will be fixed in the fullness
of time, but if that time is far away, I wonder if the libraries could
be extended to cover the obviously useful `forward' functions?  Maybe
this would be a good job for a bored undergrad...

The other thing that's a pain is that many of the mapping and folding
predicates only take HO pred's as args, rather than func's - func's
are Good Things and their use is to be encouraged, says I!

This may be leading into a debate about how far it is desirable to mix
predicate and functional styles.  Personally, I like the functional
style in many situations where explicitly having to name an
intermediate result is cumbersome.  One of the great attractions for
me of functional languages is their brevity and clarity.

Cheers,

Ralph

-- 
Ralph Becket  |  rwab1 at cam.sri.com  |  http://www.cam.sri.com/people/becket.html



More information about the users mailing list