Challenges

# Make my number a set

+3
−0

## Natural to set

(set meaning an unordered collection with no duplicates, though answers may use and output lists instead)

Recently I was brainstorming what a language with only (arbitrarily nested) sets would be like. A number is represented with a set of that many elements. The elements need to be different, which leads to this definition:

# Definition

• The set representation of $0$ is the empty set.
• The set representation of any other integer $n$ is the set of the set representations of all positive integers less than $n$, including zero.
• Negative, complex, or rational numbers do not need to be handled.
• Your full program or function is to convert from a positive integer to a set or list.

## Examples

0 {}
1 {{}}
2 {{}, {{}}}
3 {{}, {{}}, {{}, {{}}}}

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

+4
−0

# BQN, 7 6 bytesSBCS

≢↑∘⊢´↕


Run online!

The less-golfed version {↑⍟𝕩⟨⟩} is more readable: it applies Prefixes (↑) the given number of times to the empty list ⟨⟩ and… that's it. (Suffixes also works, placing elements in the opposite order). The reason this works is that a prefix of a set-number is also a set-number, so that the values of the prefixes for n range from the empty prefix 0 to the complete prefix n. This list is defined to be n+1.

The golfed version uses Fold (´) and some other tricks to turn the computation into a 3-train: something that doesn't work well with Repeat (⍟) because the number needs to be an operand.

≢↑∘⊢´↕
↕  # Range: a list with length given by the argument
≢       # Shape: an empty list as the argument has no axes
´   # Fold over the range, starting with the shape
↑      #   Prefixes
∘⊢    #   Of the right argument

Why does this post require moderator attention?
You might want to add some details to your flag.

+3
−0

# APL (ngn/apl), 6 bytes

{∇¨⍳⍵}


Try it online!

Dyalog APL and dzaima/APL appear to not give empty vectors for ⍳0, so I used ngn/APL. I don't know how to turn on boxing there, though, so there's extra processing code in the footer.

{∇¨⍳⍵}
⍵ ⍝ The input
⍳   ⍝ Make a range [1,⍵]
∇¨   ⍝ For each number in that range, run this function on it

Why does this post require moderator attention?
You might want to add some details to your flag.

+2
−0

# Python 3, 43 bytes

f=lambda x:x and[f(y)for y in range(x)]or[]


Try it online!

Unfortunately, I cannot use Python sets, because sets are unhashable and thus cannot be put into sets.

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

# Haskell, 51 bytes

data S=S[S]
f=scanl(\(S a)b->S$a++[b])(S[])$f
(f!!)


Try it online!

Ignore the deriving Show part and the g= in the TIO link - it's just for convenience. The function to use is (f!!). S is a recursive data structure, because sets of varying element types can't be represented with simple lists in Haskell. f is an infinite list of sets.

Why does this post require moderator attention?
You might want to add some details to your flag.

+1
−0

# JavaScript (V8), 32 bytes

f=n=>[...Array(n).keys()].map(f)


Try it online!

Somehow it doesn't recurse forever at n=0. Pretty nice.

Why does this post require moderator attention?
You might want to add some details to your flag.

#### 1 comment

It doesn't recurse forever because Array(0) = [], and .keys() and .map(f) don't do anything for empty lists Wezl‭ 17 days ago

+1
−0

# Jq, 19 bytes

def s:[range(.)|s];

Why does this post require moderator attention?
You might want to add some details to your flag.