Re: Subsets from a set
- From: torbenm@xxxxxxxxxxxxx (Torben Ægidius Mogensen)
- Date: 14 Aug 2006 14:40:42 +0200
"stdazi@xxxxxxxxx" <stdazi@xxxxxxxxx> writes:
What i'd like to get is something like this
set A = {2,3,4,5}
subsets(A, 2) returns :
{2,3}, {2,4}, {2,5}, {3,5}, {4,5} .....
In Haskell:
subsets xs 0 = [[]]
subsets [] n | n>0 = []
subsets (x:xs) n | n>0 = map (x:) (subsets xs (n-1)) ++ subsets xs n
Reading guide:
- square brackets are list brackets, so read them like the curly
braces of sets.
- ++ is list concatenation, so read it as U (set union)
- x:xs is the element x added to the list xs, so read it as {x} U xs.
- read "|" as "where".
If you call
subsets [2..5] 2
you get
[[2,3],[2,4],[2,5],[3,4],[3,5],[4,5]].
Torben
.
- References:
- Subsets from a set
- From: stdazi@xxxxxxxxx
- Re: Subsets from a set
- From: stush
- Re: Subsets from a set
- From: stdazi@xxxxxxxxx
- Subsets from a set
- Prev by Date: Re: Subsets from a set
- Next by Date: Is it essential to learn data structures before automata theory?
- Previous by thread: Re: Subsets from a set
- Next by thread: Re: Subsets from a set
- Index(es):
Relevant Pages
|