user3302146
user3302146

Reputation: 335

List Comprehension in haskell

I am new to Haskell and learning about lists. I have the following list [1, 2, 3] and another list [4, 5, 6]. I am trying to find a way to get the following output:

[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6]]

That is, for each element in the second list, I want to create a new list which is the first list with that element appended.

Upvotes: 1

Views: 436

Answers (1)

Elie Génard
Elie Génard

Reputation: 1683

If a = [1, 2, 3] and b = [4, 5, 6], you can do something like this:

map (\x -> a ++ [x]) b

For each element of b, map will apply the function \x -> a ++ [x]. This function concatenates two lists, a and [x].

You can also write it as a list comprehension:

[a ++ [x] | x <- b]

Upvotes: 4

Related Questions