Search code examples
haskellidiomspointfree

What is the idiomatic way to refer to a singleton list constructor in Haskell?


Short form: is there a more idiomatic way to write (\a->[a]) ?

Long form: For any data type Foo a, if I have a function f :: Foo a -> b and I need to write something like...

wrapAndF a = f $ Foo a

...I can make it point-free by writing

wrapAndF = f . Foo

But if my function g :: [a] -> b operates on lists and my wrapper looks like this...

wrapAndG a = g [a]

...what is the most idiomatic way to write it point-free? I know I can write an explicit lambda:

wrapAndG = g . (\x->[x])

or, mirroring the use of the constructor in the Foo example, use the list constructor (:), but then I have to flip the arguments:

wrapAndG = g . flip (:) []

...but what is the idiomatic way to refer to the singleton list constructor function? I expected to find a standard function with signature a -> [a], but I couldn't find it on Hoogle or Data.List.

There is of course the alternative of simply not writing it point-free (and that is certainly a valid answer) but since passing type constructors around as wrapper functions seems really useful, it felt odd that I couldn't find a standard function to wrap a value into a list, so I figured I may be missing something.


Solution

  • Echoing the comments by user2407038 and chi, (:[]) is a fine way of spelling it if you want something specific to lists:

    wrapAndG = g . (:[])