Function syntax

Ralph Becket rwab1 at cam.sri.com
Tue Sep 8 22:59:13 AEST 1998


Unless I've missed something, a function has to be written like

f(X) = Y :-
	<code deriving Y from X>.

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

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)].

There are situations where excess syntax detracts from readability.

inc_list/2 would otherwise have to be written as

inc_list(Xs) = Ys :- Ys = map((func(X) = Y :- Y = X + 1), Xs).

which just isn't as clear or succinct.

The odd carefully chosen default rule such as moding for functions
is a great help here.

[Forgive me if I've missed some brackets out, I'm a bit rusty.]

This isn't a major gripe, just a niggle.

Cheers,

Ralph

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



More information about the users mailing list