简体   繁体   中英

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM