Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powerset of a set with list comprehension in Haskell

I am a complete beginner in Haskell and I have 11 exercises for homework, 10 of which I have already solved. I have found several solutions to get the powerset of a set, but none of them includes list comprehension. I know I should not ask for a complete answer in this case (because it is homework) but I would very much appreciate any feedback/clue.

The powerset of set S is a set containing all subsets of S. Write a recursive function powerset that returns a set containing all subsets of a given set. Use direct recursion and list comprehension.

like image 944
user3059248 Avatar asked Sep 14 '15 23:09

user3059248


Video Answer


1 Answers

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
like image 144
Michael McKenna Avatar answered Nov 01 '22 10:11

Michael McKenna