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

Volker Wysk post at volker-wysk.de
Fri Mar 15 00:23:57 AEDT 2019


Am Donnerstag, 14. März 2019, 13:54:35 CET schrieb Zoltan Somogyi:
> 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 | _].

Yes, I'm aware of that. The difference is, that list.head is a function, 
whereas that deconstruction is used as a predicate. 

Bye
V.W.




More information about the users mailing list