Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Haskell, is there a built-in function that creates a list of one element?

Tags:

list

haskell

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 " ++)
like image 380
me2 Avatar asked Feb 03 '10 09:02

me2


People also ask

Does Haskell have lists?

In Haskell, lists are a homogenous data structure. It stores several elements of the same type. That means that we can have a list of integers or a list of characters but we can't have a list that has a few integers and then a few characters.

How do I print a list of elements in Haskell?

If show is ok to print your elements, you can use putStr ( unlines $ map show [(1,"A"),(2,"B"),(3,"C"),(4,"D")]) but you can replace show by any funtion that'll take your custom type and return a string.

How do you get the first element of a list in Haskell?

Finding / searching 0 will result in 1 . (Related: head xs returns the first element of the list.) (Related: last xs returns the last element of the list.)


1 Answers

Monadic return:

return x

Or:

(:[]) x

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

like image 67
Tom Lokhorst Avatar answered Nov 15 '22 06:11

Tom Lokhorst