Power set: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: Fixed mismatch between a function name and its type signature name, added a point-free formulation |
Added Kotlin |
||
Line 1,813: | Line 1,813: | ||
"ABC") |
"ABC") |
||
</lang> |
</lang> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.1.3 |
|||
class PowerSet<T>(val items: List<T>) { |
|||
private lateinit var combination: IntArray |
|||
init { |
|||
println("Power set of $items comprises:") |
|||
for (m in 0..items.size) { |
|||
combination = IntArray(m) |
|||
generate(0, m) |
|||
} |
|||
} |
|||
private fun generate(k: Int, m: Int) { |
|||
if (k >= m) { |
|||
println(combination.map { items[it] }) |
|||
} |
|||
else { |
|||
for (j in 0 until items.size) |
|||
if (k == 0 || j > combination[k - 1]) { |
|||
combination[k] = j |
|||
generate(k + 1, m) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val itemsList = listOf( |
|||
listOf(1, 2, 3, 4), |
|||
emptyList<Int>(), |
|||
listOf(emptyList<Int>()) |
|||
) |
|||
for (items in itemsList) { |
|||
PowerSet(items) |
|||
println() |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Power set of [1, 2, 3, 4] comprises: |
|||
[] |
|||
[1] |
|||
[2] |
|||
[3] |
|||
[4] |
|||
[1, 2] |
|||
[1, 3] |
|||
[1, 4] |
|||
[2, 3] |
|||
[2, 4] |
|||
[3, 4] |
|||
[1, 2, 3] |
|||
[1, 2, 4] |
|||
[1, 3, 4] |
|||
[2, 3, 4] |
|||
[1, 2, 3, 4] |
|||
Power set of [] comprises: |
|||
[] |
|||
Power set of [[]] comprises: |
|||
[] |
|||
[[]] |
|||
</pre> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |