| Bytes | Lang | Time | Link |
|---|---|---|---|
| 161 | BrainPipe | 240331T225215Z | jan |
| 019 | Uiua | 240325T221941Z | jan |
| 026 | JavaScript Node.js | 240326T021533Z | l4m2 |
| 022 | x8664 machine code | 220206T051029Z | m90 |
| 015 | Raku | 210428T112809Z | Razetime |
| 004 | Pip p | 210428T033019Z | DLosc |
| 010 | Husk | 210427T185121Z | user |
| 030 | APLNARS | 181223T204915Z | user5898 |
| 023 | Pari/GP | 181224T070326Z | alephalp |
| 014 | Underload | 181221T034235Z | ais523 |
| 074 | Batch | 170307T100611Z | Neil |
| 119 | Racket | 161003T152602Z | rnso |
| 022 | GAP | 161003T131834Z | Christia |
| 014 | Mathematica | 161003T065356Z | alephalp |
| 027 | Perl | 161003T061818Z | Ton Hosp |
| 007 | 05AB1E | 160930T214543Z | Emigna |
| 017 | Cheddar | 161001T025153Z | Downgoat |
| 018 | Retina | 161002T171345Z | Martin E |
| 014 | CJam | 160930T223829Z | Luis Men |
| 014 | Brachylog | 161002T164319Z | Fatalize |
| 031 | Mathematica | 161001T025528Z | Greg Mar |
| 016 | Perl 6 | 161001T012731Z | Brad Gil |
| 027 | Haskell | 160930T234735Z | Laikoni |
| 021 | Ruby | 160930T221613Z | Conor O& |
| 003 | Jelly | 160930T215918Z | Dennis |
| 013 | MATL | 160930T215851Z | Luis Men |
| 032 | JavaScript ES6 | 160930T220143Z | ETHprodu |
| 026 | Python 2 | 160930T215637Z | Dennis |
| 004 | Pyth | 160930T215458Z | isaacg |
BrainPipe, 185 161 bytes
-[>,----------]>>-<<<+[->++++++[->------<]>--<<[->>+<<]<+]>>>>+[-<[->++++++++++<]>>+]<.|.->,+[-[-[->+>+<<]>>[-<<+>>].<]+.-<+]-|+[++++[->++++++++<],[->+<]>.[-]<+]
Brainpipe is my Brainfuck derivative I just made where you get to glue multiple bf interpreters together at their inputs and outputs. This Program uses three such subunits:
- One to convert a number given into a byte
- One to implement the following algorithm:
Output 0 and place a marker to make a stack. Take Input onto the stack. Until the stack is empty, if the top value on the stack is not 0, output 0, decrement the value and duplicate it. If the value is 0, pop it and output 1.
- One to add 40 to each of the bits the previous has provided, which turns them into
(and).
The funny part is that the actual logic is rather short, and that most bytes are dedicated to dealing with the ASCII Decimal input and the Output.
ASCII DEC to byte -[>,----------]>>-<<<+[->++++++[->------<]>--<<[->>+<<]<+]>>>>+[-<[->++++++++++<]>>+]<.
The Algorithm |.->,+[-[-[->+>+<<]>>[-<<+>>].<]+.-<+]
Add 40 |+[++++[->++++++++<],[->+<]>.[-]<+]
Brainfuck, 192 bytes
-[>,----------]>>-<<<+[->++++++[->------<]>--<<[->>+<<]<+]>>>>+[-<[->++++++++++<]>>+]>+++++[-<++++++++>]<.[-]<<->+[-[-[->+>+<<]>>[-<<+>>]>+++++[-<++++++++>]<.[-]<]>+++++[-<++++++++>]<+.[-]<+]-
Try it online! This is basically the same thing, except the add 40 code is copy-pasted to the three output locations in the algorithm, and the same memory is used for everything.
I think you should know that the program requires a newline after the input number, which makes way less sense for the web version than for the command-line version.
x86-64 machine code, 22 bytes
31 C0 99 B0 7B AA FF C2 0F BC CA 39 F1 B0 7D F3 AA 75 F0 66 AB C3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address at which to place the result, as a null-terminated byte string, and takes the number n in RSI.
In assembly:
f: xor eax, eax #
cdq # Set EAX and EDX to 0.
r: mov al, 0x7B # Put the ASCII code of '{' into AL.
stosb # Add that to the string, advancing the pointer.
inc edx # Add 1 to EDX.
bsf ecx, edx # Set ECX to the lowest position of a 1 bit in EDX.
cmp ecx, esi # Compare that number with n.
mov al, 0x7D # Put the ASCII code of '}' in AL.
rep stosb # Add that to the string ECX times, advancing the pointer.
jne r # Repeat if the number is not equal to n.
stosw # Add two bytes from AX to the string: one more '}' and a null byte.
ret # Return.
Raku, 17 15 bytes
(),{|$_,$_}...*
An anonymous lazy list. You can assign it to a variable like:
my @list = (),{|$_,$_}...* ;
and access it with @list[n].
Pip -p, 4 bytes
fM,a
The basic recursive approach works well in Pip:
f The main function, i.e. the whole program treated as a function
M mapped to
,a range(argument)
The result is a nested list, which is printed using square brackets thanks to the -p flag.
Husk, 10 bytes
`:'}:'{ṁ₀ŀ
`:'}:'{ṁ₀ŀ
ŀ Range [0, n)
ṁ₀ For all numbers in that range, run this function on them
ṁ Then concatenate those representations together
:'{ Prepend '{'
`:'} Append '}'
APL(NARS), 15 chars, 30 bytes
{⍵=0:⍬⋄∇¨¯1+⍳⍵}
test:
f←{⍵=0:⍬⋄∇¨¯1+⍳⍵}
o←⎕fmt
o f 0
┌0─┐
│ 0│
└~─┘
o f 1
┌1───┐
│┌0─┐│
││ 0││
│└~─┘2
└∊───┘
o f 2
┌2──────────┐
│┌0─┐ ┌1───┐│
││ 0│ │┌0─┐││
│└~─┘ ││ 0│││
│ │└~─┘2│
│ └∊───┘3
└∊──────────┘
o f 3
┌3────────────────────────┐
│┌0─┐ ┌1───┐ ┌2──────────┐│
││ 0│ │┌0─┐│ │┌0─┐ ┌1───┐││
│└~─┘ ││ 0││ ││ 0│ │┌0─┐│││
│ │└~─┘2 │└~─┘ ││ 0││││
│ └∊───┘ │ │└~─┘2││
│ │ └∊───┘3│
│ └∊──────────┘4
└∊────────────────────────┘
o f 4
┌4────────────────────────────────────────────────────┐
│┌0─┐ ┌1───┐ ┌2──────────┐ ┌3────────────────────────┐│
││ 0│ │┌0─┐│ │┌0─┐ ┌1───┐│ │┌0─┐ ┌1───┐ ┌2──────────┐││
│└~─┘ ││ 0││ ││ 0│ │┌0─┐││ ││ 0│ │┌0─┐│ │┌0─┐ ┌1───┐│││
│ │└~─┘2 │└~─┘ ││ 0│││ │└~─┘ ││ 0││ ││ 0│ │┌0─┐││││
│ └∊───┘ │ │└~─┘2│ │ │└~─┘2 │└~─┘ ││ 0│││││
│ │ └∊───┘3 │ └∊───┘ │ │└~─┘2│││
│ └∊──────────┘ │ │ └∊───┘3││
│ │ └∊──────────┘4│
│ └∊────────────────────────┘5
└∊────────────────────────────────────────────────────┘
I don't know if this would be accepted...
Zilde is ⍬ here it represent the void set {}
if I want print the Zilde element or one element full of Zilde, and Zilde enclosed
all what happen is print nothing...
so for see Zilde one has to define one function I call it o (o←⎕fmt)
I do not insert in the count because the element and its structure exist
even if the sys not print it...It is possible if io is 0
{⍵=0:⍬⋄∇¨⍳⍵}
could be 12 characters solution too...
Underload, 14 bytes
((:a*)~^()~^a)
Underload full programs can't take input via any of our defined methods, so this is a function that takes input from the stack as a Church numeral (the normal way to define integers in Underload), and produces output to the stack as a string.
The (…) grouping markers are required to make this a function (reusable) rather than a snippet (only usable once). The wrapper in the TIO link calls the function in question destructively using ^, but it could be reused via making a copy of it and only consuming one of the copies when calling it. It also provides input to the program (here, (:*:*), i.e. 4), and prints the output using S.
Explanation
Underload's surprisingly suited for this task as Turing tarpits go, having such useful primitives as "copy" and "surround with parentheses". (Somehow, Underload, normally a very verbose language, is beating Mathematica, normally a language which wins due to having a huge set of builtins, via having more appropriate builtins!) Here's how the program works:
((:a*)~^()~^a)
( ) Make a snippet into a function
( )~^ Exponentiate the following function by the top of stack:
: Copy the top stack element
a Surround the copy in parentheses
* Append the copy to the original, popping the copy
~^ Run the resulting function, with the following argument on its stack:
() Empty string
a Surround the result in parentheses
Function exponentiation effectively causes the steps of the function to repeat that many times, so, e.g., (:a*)³ would be (:a*:a*:a*). That's the idiomatic way to write a loop that repeats a given number of times in Underload. (You can note that the ~^ is described two different ways above; that's because integers in Underload are defined as function exponentiation specialised to that integer, so in order to do a function exponentiation, you simply try to execute an integer as though it were a function.)
Batch, 74 bytes
@set s={}
@for /l %%i in (1,1,%1)do @call set s={%%s%%%%s:~1%%
@echo %s%
Uses the fact that each answer is equal to the previous answer inserted into itself after the leading {. The first few outputs are as follows:
{}
{{}}
{{{}}{}}
{{{{}}{}}{{}}{}}
{{{{{}}{}}{{}}{}}{{{}}{}}{{}}{}}
{{{{{{}}{}}{{}}{}}{{{}}{}}{{}}{}}{{{{}}{}}{{}}{}}{{{}}{}}{{}}{}}
Racket 119 bytes
(λ(n)(define ll(list'()))(for((i(range 1 n)))(set! ll(cons ll(for/list((j(length ll)))(list-ref ll j)))))(reverse ll))
Ungolfed:
(define f
(λ (n)
(define ll (list '()))
(for ((i (range 1 n)))
(set! ll
(cons ll
(for/list ((j (length ll)))
(list-ref ll j)
))))
(reverse ll)))
Testing (In Racket {} is same as () and default output is ()):
(f 4)
'(() (()) ((()) ()) (((()) ()) (()) ()))
To clearly see each number (0 to 3):
(for((i (f 4))) (println (reverse i)))
'()
'(())
'(() (()))
'(() (()) ((()) ()))
Mathematica, 14 bytes
Array[#0,#,0]&
Perl, 27 bytes
Includes +1 for -p
Many different methods all seem to end up as either 27 or 28 bytes. e.g.
#!/usr/bin/perl -p
$\=$_="{@F}"for@F[0..$_]}{
The best I could find is
#!/usr/bin/perl -p
s/./{$_/ for($\="{}")x$_}{
since on older perls you can drop the space before the for and get 26 bytes
05AB1E, 8 7 bytes
)IF)©`®
Explanation
) # wrap stack in a list, as stack is empty this becomes the empty list []
IF # input number of times do:
) # wrap stack in list
© # store a copy of the list in the register
` # flatten the list
® # push the copy from the register
# implicitly print top value of stack after the last loop iteration
Saved 1 byte thanks to Adnan.
Cheddar, 17 bytes
n f->(|>n).map(f)
Short recursion + Short range + Short iteration = A challenge where cheddar does very well
Non-competing, 11 bytes
n f->|>n=>f
The => operator was added after this challenge was released making this answer non-competing.
This may look confusing but let me simplify it:
n f -> |> n => f
basically n is the input and f is the function itself. |>n generates [0, n) and => maps that over f.
Retina, 24 18 bytes
.+
$*1<>
+`1<
<<$'
Try it online! (The first line enables a linefeed-separated test suite.)
Explanation
.+
$*1<>
This converts the input to unary and appends <>, the representation of 0.
+`1<
<<$'
Here, the + indicates that this substitution should be run in a loop until the string stops changing. It's easier to explain this by going through the individual steps I took golfing it down. Let's with this version of the substitution:
1<(.*)>
<<$1>$1>
This matches the last 1 of the unary representation of the remaining input (to remove it and decrement the input), as well as the contents of the current set at the end. This is then replaced with a new set containing the previous one as well as its contents. However, we can notice that $1 is followed by > in both cases and hence we can include it in the capture itself and omit it from the substitution pattern. That leads to the form
1<(.*)
<<$1$1
However, now we can observe that (.*) just captures the suffix of the string after 1< and we even reinsert this suffix at the end with $1. Since the substitution syntax gives us a way to refer to the part of a string after a match with $' we can simply omit both of those parts and end up with the version used in the answer:
1<
<<$'
CJam, 14 bytes
"[]"{_)@\]}ri*
Explanation
"[]" e# Push this string. It is the representation of 0, and also serves
e# to initialize
{ }ri* e# Repeat this block as many times as the input number
_ e# Duplicate
) e# Uncons: split into array without the last element, and last element
@\ e# Rotate, swap
] e# Pack stack contents into an array
e# Implicitly display
In each iteration, the block builds the representation of a number from that of the preceding one. To illustrate, let's consider the second iteration, where the representation of number 2 is built from that of 1, which is the string "[[]]".
- The stack contains
"[[]]" - After statement
_(duplicate) it contains"[[]]","[[]]" - After statement
)(uncons) it contains"[[]]","[[]","]" - After statement
@(rotate) it contains"[[]","]","[[]]" - After statement
\(swap) it contains"[[]","[[]]","]" - After statement
](pack into array) it contains["[[]" "[[]]" "]"], which would be displayed as the string"[[][[]]]".
Brachylog, 14 bytes
yk:{,[]:?:gi}a
Explanation
yk The range [0, ..., Input - 1]
:{ }a Apply on each element of the range
,[]:?:gi Group the empty list [] in a list Input times
Mathematica, 31 bytes
Straightforwardly implements the definition as a nested list. Uses an unnamed function that recursively calls itself using #0.
If[#<1,{},Join[t=#0[#-1],{t}]]&
Perl 6, 16 bytes
{({@_}…*)[$_]}
Returns nested data structure.
Example:
say {({@_}…*)[$_]}( 4 );
# [[] [[]] [[] [[]]] [[] [[]] [[] [[]]]]]
Explanation:
{ # lambda with implicit parameter 「$_」
(
# produce a lazy infinite sequence of all results
{ # lambda with implicit parameter 「@_」
@_ # array containing all previously seen values in the sequence
}
… # keep repeating that block until:
* # Whatever ( never stop )
)[ $_ ] # use the outer block's argument to index into the sequence
}
Ruby, 27 21 bytes
I'm new to ruby golfing, but here goes nothing. Thanks to Jordan for saving 6 bytes!
f=->s{(0...s).map &f}
This is a recursive function f (a proc, to be specific) and takes an argument s. It maps the proc f over 0...s, which is the range [0, s).
Jelly, 3 bytes
Ḷ߀
This is a monadic link. Try it online!
How it works
Each natural number is the set of all previous natural numbers, i.e., n = {0, …, n-1}. Since there are no natural numbers preceding 0, we have that 0 = {}.
Ḷ߀ Monadic link. Argument: n (natural number)
Ḷ Unlength; yield [0, ..., n-1].
߀ Recursively map this link over the range.
MATL, 13 bytes
Xhi:"tY:Xh]&D
Explanation
Xh % Concatenate the stack contents into cell array. Since the stack
% is empty, this produces the empty cell array, {}
i:" ] % Take input number. Repeat that many times
t % Duplicate the cell array that is at the top of the stack
Y: % Unbox it, i.e., push its contents onto the stack
Xh % Concatenate the stack contents into a cell array
&D % String representation. Implicitly display
JavaScript (ES6), 32 bytes
f=n=>[...Array(n).keys()].map(f)
Simple enough.
Pyth, 4 bytes
LyMb
L: Define the function y with input b
yMb: y mapped over the range 0, 1, ..., b-1
On the input 0, this map returns []. Otherwise, it returns y mapped over all numbers up to b.