Powerset Combo List in Haskell

I'm a complete newbie to Haskell, and I have 11 homework exercises, 10 of which I have already decided. I found several solutions for getting a set of set parameters, but none of them include list comprehension. I know that I should not ask for a complete answer in this case (because this is homework), but I would really appreciate any feedback / tips.

The strength of the set of the S - is a set containing all subsets of the S . Write a recursive function powersetthat returns a collection containing all subsets of that collection. Use direct recursion and list comprehension.

+4
source share
3 answers

Ok, here is my tip:

If you look at something like (x:xs), now you have the choice to either include xin the subset or not.

Somehow you have to use both options (maybe with (++);)) ...

Now remember the other clues (recursion ... xs....), and maybe you get an idea if you think about[x:ys | ys <- ...]


By the way: this is almost deceiving, but if you find a solution using the notation do: it is very easy to translate into a list of concepts;) - maybe you can publish your progress a little?

+4
source

Using direct recursion and list comprehension:

type Set a = [a]

powerset :: Set a -> Set (Set a)
powerset [] = [[]]
powerset (x:xs) = [x:ps | ps <- powerset xs] ++ powerset xs
+3
source

" ". , subsets (x:xs), subsets xs . xs x:xs, , ?

+1

Source: https://habr.com/ru/post/1607363/


All Articles