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

Zoltan Somogyi zoltan.somogyi at runbox.com
Thu Mar 14 23:54:35 AEDT 2019



On Thu, 14 Mar 2019 09:03:09 +0100, Volker Wysk <post at volker-wysk.de> wrote:
> 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?

Yes, but if the compiler knows that e.g. List is nonempty, then there is a simpler way
to get its first element than calling list.head: you can just write List = [Head | _].

Zoltan.


More information about the users mailing list