Proposal: Add singleton function to Data.List module

Tikhon Jelvis tikhon at jelv.is
Tue Aug 13 08:33:23 UTC 2019


Another advantage to having an explicit singleton function is
discoverablity. I distinctly remember looking for exactly this function
when I was a beginner and being confused because it didn't seem to exist.

Idioms like (:[]) are not intuitive at all. Polymorphic functions like pure
require you to know that list is an Applicative, which you won't learn
right away. Even if your have learned about Applicatives, it's easy to miss
that connection when you're thinking about lists as a data structure rather
than as a control structure, which is exactly the scenario I expect people
to use a singleton function.

On Tue, Aug 13, 2019, 09:44 Matthew Pickering <matthewtpickering at gmail.com>
wrote:

> Clear +1 from me. I have defined this function many times.
>
> The addition of a one line function does not prevent anyone from using
> `pure` in their own code if they wish to obfuscate matters.
>
> Cheers,
>
> Matt
>
> On Tue, Aug 13, 2019 at 6:35 AM Elliot Cameron <eacameron at gmail.com>
> wrote:
> >
> > Seq is applicative and also has singleton. Some people like to avoid
> polymorphism when the use case is clearly monomorphic (c.f. discussion
> about map vs fmap).
> >
> > I'd like to point out that Pointed is sort of the abstraction we really
> want for this... But I still want a monomorphic function that doesn't delve
> into Kmett dependencies.
> >
> > On Mon, Aug 12, 2019 at 11:59 PM George Wilson <george at wils.online>
> wrote:
> >>
> >> Weak +1, I'd want it in NonEmpty too if it's going into List.
> >>
> >> On Tue, 13 Aug 2019 at 13:37, Joseph C. Sible <josephcsible at gmail.com>
> wrote:
> >> >
> >> > -1. AFAICT, "Polymorphic, works for any `Functor`" is an upside of
> >> > just using `pure`, not a downside. (Please correct me if I'm wrong,
> >> > and there is some disadvantage that I don't see.) Also, it looks like
> >> > for everything that has a `singleton`, it's something that isn't an
> >> > applicative functor, so they have them instead of `pure`, not in
> >> > addition to it.
> >> >
> >> > Joseph C. Sible
> >> >
> >> > On Mon, Aug 12, 2019 at 12:14 PM Taylor Fausak <taylor at fausak.me>
> wrote:
> >> > >
> >> > > I originally made this suggestion on GitLab, but I was told to make
> it here instead.
> >> > >
> >> > > https://gitlab.haskell.org/ghc/ghc/issues/17042
> >> > >
> >> > > ---
> >> > >
> >> > > # Add list singleton function
> >> > >
> >> > > ## Motivation
> >> > >
> >> > > Sometimes it is convenient to have a function to wrap an element in
> a list. There are many ways to do this already, but none of them are as
> clear as a separate monomorphic function.
> >> > >
> >> > > - `pure`: Polymorphic, works for any `Functor`.
> >> > > - `pure @[]`: Noisy, requires `-XTypeApplications`.
> >> > > - `(: [])`: Subjectively ugly.
> >> > > - `(\x -> [x])`: Syntactically noisy.
> >> > >
> >> > > This Twitter thread includes some additional commentary:
> https://twitter.com/taylorfausak/status/1159264862247280640
> >> > >
> >> > > ## Proposal
> >> > >
> >> > > I would like to add a `singleton` function to `Data.List` that
> mirrors the `singleton` function for other containers:
> https://www.stackage.org/lts-14.0/hoogle?q=singleton
> >> > >
> >> > > ``` hs
> >> > > singleton :: a -> [a]
> >> > > singleton x = [x]
> >> > > ```
> >> > >
> >> > > Other Haskell-like languages include this function:
> >> > >
> >> > > - PureScript:
> https://pursuit.purescript.org/packages/purescript-lists/5.4.0/docs/Data.List#v:singleton
> >> > > - Elm:
> https://package.elm-lang.org/packages/elm/core/latest/List#singleton
> >> > > _______________________________________________
> >> > > Libraries mailing list
> >> > > Libraries at haskell.org
> >> > > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> >> > _______________________________________________
> >> > Libraries mailing list
> >> > Libraries at haskell.org
> >> > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> >> _______________________________________________
> >> Libraries mailing list
> >> Libraries at haskell.org
> >> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> >
> > _______________________________________________
> > Libraries mailing list
> > Libraries at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20190813/993a4c44/attachment.html>


More information about the Libraries mailing list