That simple. I want to generate all sublists of a list using list comprehension.
i.e: getSublist [1,2,3] is [[1], [2], [3], [1,2], [1,3], [2, 3], [1,2,3]]
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is already implemented as
Data.List.subsequences, but if you want to define it yourself (for learning purposes), you can do it like this:You can’t do it with only list comprehensions, but with some recursion it looks like this:
Read: The only sublist of the empty list is the empty list. The sublists of
x:xs(i.e. the list with the headxand the tailxs) are all of the sublists ofxsas well as each of the sublists ofxswithxprepended to them.