Proposal: Add singleton function to Data.List module

Herbert Valerio Riedel hvriedel at gmail.com
Tue Aug 13 22:13:17 UTC 2019


>  There is a concept here: create a container containing exactly one value. As Tikhon indicates, this is a different concept from pure.

Fwiw, if you actually want a possibly meaningful/lawful reification of
the very specific "create any container containing exactly one item"
concept then an obvious approach would be to base it on the existing
(IMO under-appreciated) 'IsList' abstraction; in other words basically
something like

    singleton :: IsList l => Item l -> l
    singleton = fromListN 1 . (:[])

which could either be a method of `IsList` (with the default impl
above and an obvious lawful relationship to `fromList`/`toList`) or
just be a top-level binding exported from "GHC.List" and/or "GHC.Exts"
(which aren't governed by the Haskell Library Report and thus have a
lower barrier to entry).


More information about the Libraries mailing list