[mercury-users] Function syntax

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Sep 8 23:29:46 AEST 1998


On 08-Sep-1998, Ralph Becket <rwab1 at cam.sri.com> wrote:
> Unless I've missed something, a function has to be written like
> 
> f(X) = Y :-
> 	<code deriving Y from X>.

You missing something.  Just as predicates can have both facts
and rules, so can functions.

> Now, if the code in question is of the form
> 
> f(X) = Y :-
> 	Y = <func on X>.
> 
> is there any reason why the syntax does not also admit
> 
> f(X) = <func on X>.

That syntax is indeed allowed.

> I ask because the latter looks more natural, particularly for
> anonymous functions when using higher-order code.  For example, I'd
> like to be able to write
> 
> :- func inc_list(list(int)) = list(int).
> inc_list(Xs) = map((func(X) = X+1), Xs).
> 
> :- func map(func(int) = int, list(int)) = list(int).
> map(_, [])       = [].
> map(F, [X | Xs]) = [f(X) | map(F, Xs)].

And indeed you can.

Cheers,
	Fergus.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the users mailing list