4 ms·
Why would you use Data.List.head rather than just matching on lst?
by ImprobableTruth 6y ago
Why would you use Data.List.head rather than just matching on lst?
- runeks 6y agoI realized the example doesn't make sense for head, since head should always just return the first item of the tuple. I 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.
- ImprobableTruth 6y agoYou can avoid the error of Data.List.last by 'inlining' it (just like you could in your first example) last :: NonEmpty a -> a last (NonEmpty (a, [])) = a last (NonEmpty (a, x:[])) = x last (NonEmpty (a, x:xs)) = last (NonEmpty (a, xs))
- runeks 6y agoI see your point. But I’d argue that you avoid it in both cases, since without inlining the error-call is unreachable anyway.