tags:

views:

176

answers:

2

Looking for a built-in function that will do the following:

mklist x = [x]

The benefit is that I can use it in a composition to create a list of one element. Understand that (replicate 1) is available but is there a more direct function? Would be useful in situations like this:

["Alice", "Bob", "Charlie"] >>= mklist . ("Hello " ++)
+9  A: 

Monadic return:

return x

Or:

(:[]) x

It's less characters, but more shift-key usage, so might be harder to type.

Tom Lokhorst
Oh. Right. Duh. Thanks.
me2
:-) Of course your example could just as easily be done using `map`, but I'm assuming your real scenario is more complex.
Tom Lokhorst
`pure` from Control.Applicative also works. That's four non-shifted letters! As with `return` any type errors may get more complicated, though.
yatima2975
Yeah, it's a shame `pure` isn't in the Prelude, though. But if you do have Control.Applicative imported anyway, then `pure` is nice and short, just like we Haskell programmers like.
Tom Lokhorst
Ooh, monadic `return`. +1 for sneakiness and code obfuscation!
Norman Ramsey
+1  A: 

In case you're not familiar with hoogle:

http://www.haskell.org/hoogle/?q=a+-%3E+[a]&format=sherlock

jberryman