Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 381
Hisham H M Avatar asked Mar 07 '17 23:03

Hisham H M


People also ask

What is a singleton list in Haskell?

The list-singleton is a Haskell package allows you to easily and clearly create lists with only one element in them, which are typically called "singleton lists" or simply "singletons".

What does () mean in Haskell?

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .

What is the use of singletonlist in Java?

The singletonList() method of java.util.Collections class is used to return an immutable list containing only the specified object. The returned list is serializable. Syntax: Parameters: This method takes the object o as a parameter to be stored in the returned list.

What is a singleton and an object(in Kotlin)?

What is a Singleton and an object (in Kotlin)? The singleton pattern restricts the instantiation of a class to a single object. It is useful in cases wherein you only need a single object to contain a global state. Access is through static methods that ensures a single instance

What are the different types of Singleton implementations?

There are two types of singleton implementations: eager and lazy initialization. They differ in the way they initialize the singleton instance. We must also consider thread-safety in each of them.

How many elements are in a singleton list?

This list will always contain only one element thus the name singleton list. When we try to add/remove an element on the returned singleton list, it would give UnsupportedOperationException. Attention reader! Don’t stop learning now.


2 Answers

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

wrapAndG = g . (:[])
like image 137
2 revs Avatar answered Nov 15 '22 05:11

2 revs


For lists pure does what you want:

wrapAndG = g . pure
like image 44
Lee Avatar answered Nov 15 '22 07:11

Lee