Power set: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Correction to preamble.)
(Updated to Nim 1.4: changed initSet to initHashSet, to Set to toHashSet. Also changed some variable names and some other things.)
Line 2,401: Line 2,401:
=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import sets, hashes
<lang nim>import sets, hashes

proc hash(x: HashSet[int]): Hash =
proc hash(x: HashSet[int]): Hash =
var h = 0
var h = 0
for i in x: h = h !& hash(i)
for i in x: h = h !& hash(i)
result = !$h
result = !$h

proc powerset[T](inset: HashSet[T]): auto =
proc powerset[T](inset: HashSet[T]): HashSet[HashSet[T]] =
result = toSet([initSet[T]()])
result.incl(initHashSet[T]()) # Initialized with empty set.
for val in inset:

let previous = result
for i in inset:
var tmp = result
for aSet in previous:
for j in result:
var newSet = aSet
var k = j
newSet.incl(val)
k.incl(i)
result.incl(newSet)
tmp.incl(k)
echo powerset([1,2,3,4].toHashSet())</lang>
result = tmp

echo powerset(toSet([1,2,3,4]))</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==