What is the idiomatic way to reference 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 -> band I need to write something like ...

wrapAndF a = f $ Foo a

... I can make it silent by writing

wrapAndF = f . Foo

But if my function g :: [a] -> bworks with lists, and my cover looks like this ...

wrapAndG a = g [a]

... What is the most idiomatic way to write this for free? I know I can write an explicit lambda:

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

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

wrapAndG = g . flip (:) []

... ? a -> [a], Hoogle Data.List.

, , ( , , ), - , , , , - .

+4
3

2407038 chi, (:[]) - , - :

wrapAndG = g . (:[])
+10

pure , :

wrapAndG = g . pure
+10

Just found the answer, thanks to pointfree.io , which was related to another question. A nice, easy way to write wrapAndGis

wrapAndG = g . return

This exploits the fact that lists are instances Monad.

+3
source

Source: https://habr.com/ru/post/1671755/


All Articles