by runeks · 6y ago · 0 comments
newtype NonEmpty a = NonEmpty [a]
to: newtype NonEmpty a = NonEmpty (a, [a])
(and changing the associated functions accordingly -- which can now be implemented without the use of error).EDIT: Upon further thought, I'm not sure the above distinction makes sense. The implementation of e.g. Data.List.NonEmpty.Newtype.head would still involve the use of "error" through the use of Data.List.head:
head :: NonEmpty a -> a
head (NonEmpty (x, [])) = x
head (NonEmpty (_, lst)) = Data.List.head lstI still think it would make sense to define e.g. last using Data.List.last:
last :: NonEmpty a -> a
last (NonEmpty (a, [])) = a
last (NonEmpty (_, lst)) = Data.List.last lst
which is safe even though Data.List.last contains error. last :: NonEmpty a -> a
last (NonEmpty (a, [])) = a
last (NonEmpty (a, x:[])) = x
last (NonEmpty (a, x:xs)) = last (NonEmpty (a, xs))