[m-users.] Missing mode for list.head?

Volker Wysk post at volker-wysk.de
Thu Mar 14 19:03:09 AEDT 2019


Hi!

The list.head function can fail, because of the way it is implemented, even 
when it is known at compile time that the argument list is not empty:

:- func head(list(T)) = T is semidet.
head([X | _]) = X.

There is no mode declaration for head in list.m.

But it could be implemented like this:

:- func head1(list(T)) = T.
:- mode head1(in) = out is semidet.
:- mode head1(in(non_empty_list)) = out is det.
head1([A|_]) = A.

This way you can have a deterministic version of head, when it is known that 
it input list isn't empty.

Am I right?

Bye
Volker




More information about the users mailing list