Skip to content

Power set

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Collections.Iterable
use Collections.List
use Collections.LIST
use Ghul.Pipes
power_set[T](values: List[T]) -> LIST[LIST[T]] is
if values.count == 0 then
let empty: LIST[LIST[T]] = _()
empty.add(LIST[T]())
return empty
fi
let first = values[0]
let result: LIST[LIST[T]] = _()
for subset in power_set(values |> skip(1) |> collect()) do
let with_first: LIST[T] = _()
with_first.add(first)
with_first.add_range(subset)
result.add(subset)
result.add(with_first)
od
return result
si
braced[T](values: Iterable[T]) -> string => "{{{values |> join(", ")}}}"
show[T](sets: LIST[LIST[T]]) -> string =>
braced(sets |> map(subset => braced(subset)))
write_line(show(power_set(LIST[int]())))
write_line(show(power_set([1, 2, 3] |> collect())))
write_line(show(power_set(["a", "b"] |> collect())))