Home on the Range of Lists

This challenge is simply to return a list of lists of integers, similar to the Python range function, except that each successive number must be that deep in lists.

Rules:

• Create a program or a non-anonymous function
• It should return or print the result
• The result should be returned in a list (of lists) or array (of arrays)
• If the parameter is zero, return an empty list
• This should be able to handle an integer parameter 0 <= n < 70.
• (recursive solutions blow up pretty fast)
• The function should be callable with only the one parameter.
• Other behavior is undefined.
• This is code golf, so shortest code wins.

Example Call:

rangeList(6)
> [0, [1, [2, [3, [4, [5]]]]]]


Test Cases:

0  => []
1  => [0]
2  => [0, [1]]
6  => [0, [1, [2, [3, [4, [5]]]]]]
26 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25]]]]]]]]]]]]]]]]]]]]]]]]]]
69 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]


EDIT: isaacg's answer is the shortest so far. I'll update the accepted answer if anyone finds a shorter one in a language that existed at the posting of the challenge. Thanks for playing!

• Random comment: It's funny how the character minimum for a title is 15, and I couldn't use "Range of Lists", so I came up with this one on the spot. – mbomb007 Mar 4 '15 at 15:48
• That's mostly to prevent people from writing unassigned anonymous functions. Personally, I'd prefer it if it was a function that takes a parameter. – mbomb007 Mar 4 '15 at 16:20
• Is it allowed to create two functions, where one is a helper function? – ProgramFOX Mar 4 '15 at 18:58
• @ProgramFOX Yes. I think code external to your function is fine, since if someone wanted to import math in Python for example, I don't think it could occur inside a function. – mbomb007 Mar 4 '15 at 19:42
• @DevonParsons There are plenty of questions that have an example program contained within, but okay. – mbomb007 Mar 4 '15 at 20:21

Pyth, 13 bytes

?hu]+HG_UQYQY


Try it here.

                 Implicit:
Q = eval(input())
Y = []
?           QY   If Q = 0, print Y
h               else, print the first element of
u     _UQY     the reduce, where Y is the initial value, over the list
reversed(range(Q))
]+HG          The reduce function: The list containing H prepended onto G.
The new number is inserted into the accumulated list,
then the resultant list is wrapped in another list.


APL (13 18)

Assuming ⎕IO=0:

f←{×⍵:⊃,∘⊂∘,/⍳⍵⋄⍬}


Explanation:

• ×⍵: if ⍵ is positive,
• ,∘⊂∘,: join the left operand to the enclose of the right operand (i.e. x ,∘⊂∘, y = [x, [y]])
• /: reduce
• ⍳⍵: the numbers 0..⍵-1
• ⊃: disclose the result
• ⋄: otherwise
• ⍬: return the empty list
• (this is necessary because / fails on ⍬, and ⍳0 gives the empty list.)

This function returns a nested array. However, it is a bit hard to tell this from APL's default output. It separates array items by spaces, so you can only tell nesting by double spaces. Here is a function that will take a nested array, and return a string, formatting the nested array in Python style (i.e. [a,[b,[c,...]]]).

arrfmt←{0=≡⍵:⍕⍵ ⋄ '[',(1↓∊',',¨∇¨⍵),']'}

• I think you need another ∘, after the enclose, otherwise (at least in my interpreter - dyalog14) the last element isn't enclosed. e.g. [0 [1 [2 3]]] – Moris Zucca Mar 5 '15 at 15:16
• @marinus Can you please verify this? – mbomb007 Mar 6 '15 at 19:48
• I changed the problem statement a day or two ago to clarify that defined functions should be assigned to a variable. You should add f← to the start of your program unless you modify it to accept user input. – mbomb007 Mar 6 '15 at 21:24
• Also, the output doesn't show how deep in the list a number is vary clearly... is each space an implied bracket? – mbomb007 Mar 6 '15 at 21:25
• @MorisZucca I have to agree. See here: ngn.github.io/apl/web/#code=%7B%D7%u2375%3A%2C%u2218%u2282/… – mbomb007 Mar 6 '15 at 21:28

data L=E|I Int|L[L]
1#m=L[I$m-1] n#m=L[I$m-n,(n-1)#m]
p 0=E
p n=n#n


In Haskell all elements of a list have to be of the same type, so I cannot mix integers with list of integers and I have to define a custom list type L. The helper function # recursively constructs the required list. The main function p checks for the empty list and calls # otherwise.

As new data types cannot be printed by default (the rules allow just to return the list), I add some more code for demonstration purpose:

data L=E|I Int|L[L] deriving Show


Now:

-- mapM_ (print . p) [0..5]
E
L [I 0]
L [I 0,L [I 1]]
L [I 0,L [I 1,L [I 2]]]
L [I 0,L [I 1,L [I 2,L [I 3]]]]
L [I 0,L [I 1,L [I 2,L [I 3,L [I 4]]]]]


Python, 48 bytes

f=lambda n,i=0:i<n and[i]+[f(n,i+1)]*(i<n-1)or[]


Using list multiplication to handle the special case.

• I don't think this is Python 2 specific - seems to work in all pythons. – isaacg Mar 4 '15 at 22:24
• @isaacg Fixed. My original submission wasn't though :) – Sp3000 Mar 4 '15 at 23:23
• A tiny char-save:*(i<n-1) can be done as [:n+~i], since it's a singleton list. – xnor Mar 5 '15 at 0:01

Mathematica, 33

f@0={};f@1={0};f@n_:={0,f[n-1]+1}

• It looks so simple! – mbomb007 Mar 4 '15 at 20:08

CJam, 16 bytes

Lri){[}%]~;']*~p


This is a full program. It takes input via STDIN and prints the final array on to STDOUT.

As with the other CJam entry, 0 input will print "" as that is the representation of an empty array in CJam.

How it works:

L                   "Put an empty array on stack. This will be used for the 0 input";
ri)                "Read the input, convert it to integer and increment it";
{[}%            "Map over the array [0 ... input number] starting another array";
"after each element";
]~;         "Now on stack, we have input number, an empty array and the final";
"opening bracket. Close that array, unwrap it and pop the empty array";
']*~     "Put a string containing input number of ] characters and eval it";
"This closes all the opened arrays in the map earlier";
p    "Print the string representation of the array";
"If the input was 0, the map runs 1 time and the ; pops that 1 array";
"Thus leaving only the initial empty array on stack";


Try it online here

JavaScript (ES6) 40

Recursive solution, pretty robust, no blows. Update Fails near 6500 with 'too much recursion'

F=n=>n--?(R=m=>m<n?[m,R(++m)]:[m])(0):[]


Iterative solution (45) No limits except memory usage

F=n=>{for(s=n?[--n]:[];n;)s=[--n,s];return s}


Try F(1000): FireBug console will not show you more than 190 nested arrays, but they are there

Java, 88107105104 102 bytes

import java.util.*;int o;List f(final int n){return new Stack(){{add(n<1?"":o++);if(o<n)add(f(n));}};}


Pretty long compared to the others, though you can't do much better with Java. A check to determine whether to continue recursion is all it takes.

• You need to import java.util.*; for this to be self-containing (or fully qualify java.util.List and java.util.Stack, but that's much longer). +19 to make it 107, still 7 better than the Java answer I was working on :D – Geobits Mar 4 '15 at 17:07
• I see two you can save: o!=n can be o<n, and you can swap the ternary to o<n?o++:"". – Geobits Mar 4 '15 at 18:01
• In Java 8, I believe the final on int n can be removed. – Justin Mar 5 '15 at 17:57

Python 2, 56 bytes

I suspect this could be golfed more.

f=lambda n,i=0:[i,f(n,i+1)]if i<n-1 else[i]if n>0 else[]


Tests:

# for n in (0,1,2,6,26,69): print n, '=>', f(n)
0 => []
1 => [0]
2 => [0, [1]]
6 => [0, [1, [2, [3, [4, [5]]]]]]
26 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25]]]]]]]]]]]]]]]]]]]]]]]]]]
69 => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

• Well, you beat my Python solution. – mbomb007 Mar 4 '15 at 16:02

CJam, 17 bytes

I know Optimizer has found 16, but here is the best I can do:

{:I{[}%;{]}I1e>*}


This is a block, the closest thing to a function in CJam, which takes an integer on the stack, and leaves the desired nested array.

Use this program to test it, which puts the input on the stack, then calls the function and inspects the stack. Note that for 0, the stack output will contain "" - this is CJam's native representation of an empty array.

Ruby 46

f=->n{r=a=[]
(0...n).map{|i|a<<a=[i]}
r[0]||r}


Test it online: http://ideone.com/uYRVTa

C# - 100

Simple recursion. Check the zero special case and tick up with one variable, down with the other

object[]A(int y,int x=0){return y==0?new object[0]:y==1?new object[]{x}:new object[]{x,A(--y,++x)};}


C++ 87

(Visual C++ 2012)

int*A(int y,int x=0){int*b=new int{x};return!y?new int:--y?(b[1]=(int)A(y,++x))?b:0:b;}


This one is great, by which I mean byzantine, but it's the same basic idea as the c# one.

It's a C style array implementation, so it doesn't give you an array, it gives a int pointer, in which I was be storing both ints and other pointers. Like this: [0,*] *->[1,#] #-> [2,&] &-> etc, where the symbols are pseudo code for the int value of a pointer and the --> is where it pointers to in memory.

What an excellent easy to use implementation of c style jagged arrays I've devised (cough), but I maintain it's plausible enough to be within the rules of the question.

There's quite a lot of abusing ternary operators here, and also quite a lot of abusing the implicit cast from int to bool.

Example: If we let int *bar = (int*)A(3);, we can see:

bar
0x003bded8 {0}
((int*)bar[1])[0]
1
((int*)(((int*)bar[1])[1]))[0]
2


Which is pointer talk for [0,[1,[2]]].

Okay, fine. It doesn't actually have to be the awful. Here's some test code for running this c++ code:

int* GetNext(int* p){
return (int*)p[1];
}

int main()
{
auto x = 10;
auto bar = A(x);

for (int i = 1; i < x; i++){
bar = GetNext(bar);
std::cout << bar[0] << std::endl;
}


}

• The C++ version doesn't compile. ideone.com/fmcXYP – Anmol Singh Jaggi Mar 6 '15 at 10:27
• You should mention the compiler used alongside C++. – Anmol Singh Jaggi Mar 7 '15 at 4:59
• @anmolSinghJaggi Yeah, good idea. Visual C++ 2012, which is mostly C++11 compliant. – Nathan Cooper Mar 7 '15 at 10:29
• Old post, but with some tinkering I got it down to 86. Array g(params object[]a)=>a;Array f(int y,int x=0)=>y<1?g():y<2?g(x):g(x,f(y-1,x+1)); – dana Jan 15 '19 at 23:02

Pyth, 15 bytes

?u[HG)_UtQ]tQQY


Which is really saying, in Python:

Q = eval(input())
if Q:
print reduce(lambda G,H:[H,G], reverse(range(Q-1)), [Q-1])
else:
print []

• Hey, I'm glad you're learning Pyth! If you want to generate the evaluated input, you can use Q, which does that for you. Also, Y is preinitialized to []. – isaacg Mar 4 '15 at 16:53
• qJ_1 is the same as !Q. And JtQ actually wastes 1 byte. ?Y!Qu[HG)_UtQ[tQ – Jakube Mar 5 '15 at 0:17
• Okay I'll take those bytes. – swstephe Mar 5 '15 at 16:06
• @swstephe If you change [tQ to ]tQ, which is equivalent, you swap to order of operations of ?, so you can replace !Q with Q. This results in ?u[HG)_UtQ]tQQY - 1 more byte saved. – isaacg Mar 8 '15 at 8:55

Haskell, 65 59 45 41 bytes

These nested lists are the same data-structure as rooted Trees, except that they can also be empty. Hence, we can use a list of them - also called a Forest to represent them.

(0!)
data T=N[T]Int
m!n=[N((m+1)!n)m|m<n]


Try it online!

Explanation

First of all we need to implement the Tree data type:

data Tree = Node [Tree] Int


From there it's just recursion using two parameters m (counting up) and n to keep track when to terminate:

m ! n= [ Node ((m+1)!n) m| m<n ]


Alternative, 61 bytes

import Data.Tree
f n=unfoldForest(\b->(b,[b+1|b<n-1]))[0|n>0]


Try it online!

Explanation

The function unfoldForest takes a list of initial values and a function x -> (y,[x]). For each initial value x it unfolds a tree using the function, producing a tuple (y,xs) where y will become the root and the xs are used to repeat the procedure:

unfoldForest (\b -> (b, [b+1 | b < 2]) [0]
≡ Node 0 [unfoldForest (\b -> (b, [b+1 | b < 2) [1]]
≡ Node 0 [Node 1 [unfoldForest (\b -> (b, [b+1 | b < 2) []]]
≡ Node 0 [Node 1 []]


sub t{$r=[($t)=@_];$r=[$t,$r]while--$t>0;$r}  Will add explanation upon request. You can try it here. • I'm wondering, because I'm not familiar with Perl - Does the most deeply nested array have 2 elements in it, one being nil or whatever the equivalent is? I ask because on the page you link to the innermost array looks like (3,) – Devon Parsons Mar 4 '15 at 18:40 • @DevonParsons the code I added to print it in a readable way adds a comma after each element. undef is the equilvalent of nil or null in Perl and there is not an extra element. Perl flattens arrays so this is creating nested array references. – hmatt1 Mar 4 '15 at 18:45 JavaScript, 93 bytes This isn't ideal, but I might as well give it a try. I'll try and golf this further later on, though for now I see no obvious way to. function f(n){s='[';i=0;while(i<n-1)s+=i+++',[';s+=i||'';do{s+=']'}while(i--);return eval(s)}  • You might also try creating a recursive solution, as it might be shorter. – mbomb007 Mar 4 '15 at 19:45 Python, 75 bytes This is just for show. It's the program I wrote when creating/designing this challenge. f=lambda x,y=[]:y if x<1 else f(x-1,[x-2]+[y or[x-1]])if x>1 else y or[x-1]  Python, 44 f=lambda n,i=0:i<n-1and[i,f(n,i+1)]or[i][:n]  Recursively creates the tree. The [:n] at the end is to special-case n==0 into giving the empty list. • It was during this challenge that I realized that and and or can have spaces omitted next to integers, but else cannot. – mbomb007 Mar 6 '15 at 19:47 • @mbomb007 It's because else starts with e, and things like 1e6 are valid number literals. – xnor Mar 6 '15 at 22:43 • I knew that, but I didn't know that was why. Thanks. – mbomb007 Mar 6 '15 at 22:53 • @mbomb007 Actually after 2.6 or 2.7 or so you can lose the space before else, e.g. x = 1 if y==2else 5 works. – Sp3000 Mar 15 '15 at 2:20 • It doesn't work in Python 2.7.2 repl.it/eB6 (But it does work in 3.4) – mbomb007 Mar 15 '15 at 4:21 Joe, 8 bytes Note: This is a non-competing answer. The first version of Joe was released after this question. F:/+,M]R  What do we have here? F: defines a function F that is a chain of /+,, M] and R. When you call Fn, first Rn gets evaluated, returning range from 0 to n, exclusive. M] wraps each element to a list. Then the list is applied to /+,. x +, y returns x + [y]. / is a right fold. Thus, /+,a b c d... returns [a, [b, [c, [d...]]]. Example invocations (code is indented by 3, output by 0):  F:/+,M]R F10 [0, [1, [2, [3, [4, [5, [6, [7, [8, [9]]]]]]]]]] F2 [0, [1]] F1 [0] F0 [] F_5 [0, [-1, [-2, [-3, [-4]]]]]  Ruby - Recursive version - 52 r=->(n,v=nil){(n-=1;n<0 ?v:r[n,(v ?[n,v]:[n])])||[]}  Non-recursive version: 6662 57 r=->i{(i-1).downto(0).inject(nil){|a,n|a ?[n,a]:[n]}||[]}  Sample output (same for both versions) p r[0] # => [] p r[1] # => [0] p r[2] # => [0, [1]] p r[6] # => [0, [1, [2, [3, [4, [5]]]]]] p r[26] # => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25]]]]]]]]]]]]]]]]]]]]]]]]]] p r[69] # => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]  The non-recursive version can handle arbitrarily large input. p r[1000] # => [0, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, [21, [22, [23, [24, [25, [26, [27, [28, [29, [30, [31, [32, [33, [34, [35, [36, [37, [38, [39, [40, [41, [42, [43, [44, [45, [46, [47, [48, [49, [50, [51, [52, [53, [54, [55, [56, [57, [58, [59, [60, [61, [62, [63, [64, [65, [66, [67, [68, [69, [70, [71, [72, [73, [74, [75, [76, [77, [78, [79, [80, [81, [82, [83, [84, [85, [86, [87, [88, [89, [90, [91, [92, [93, [94, [95, [96, [97, [98, [99, [100, [101, [102, [103, [104, [105, [106, [107, [108, [109, [110, [111, [112, [113, [114, [115, [116, [117, [118, [119, [120, [121, [122, [123, [124, [125, [126, [127, [128, [129, [130, [131, [132, [133, [134, [135, [136, [137, [138, [139, [140, [141, [142, [143, [144, [145, [146, [147, [148, [149, [150, [151, [152, [153, [154, [155, [156, [157, [158, [159, [160, [161, [162, [163, [164, [165, [166, [167, [168, [169, [170, [171, [172, [173, [174, [175, [176, [177, [178, [179, [180, [181, [182, [183, [184, [185, [186, [187, [188, [189, [190, [191, [192, [193, [194, [195, [196, [197, [198, [199, [200, [201, [202, [203, [204, [205, [206, [207, [208, [209, [210, [211, [212, [213, [214, [215, [216, [217, [218, [219, [220, [221, [222, [223, [224, [225, [226, [227, [228, [229, [230, [231, [232, [233, [234, [235, [236, [237, [238, [239, [240, [241, [242, [243, [244, [245, [246, [247, [248, [249, [250, [251, [252, [253, [254, [255, [256, [257, [258, [259, [260, [261, [262, [263, [264, [265, [266, [267, [268, [269, [270, [271, [272, [273, [274, [275, [276, [277, [278, [279, [280, [281, [282, [283, [284, [285, [286, [287, [288, [289, [290, [291, [292, [293, [294, [295, [296, [297, [298, [299, [300, [301, [302, [303, [304, [305, [306, [307, [308, [309, [310, [311, [312, [313, [314, [315, [316, [317, [318, [319, [320, [321, [322, [323, [324, [325, [326, [327, [328, [329, [330, [331, [332, [333, [334, [335, [336, [337, [338, [339, [340, [341, [342, [343, [344, [345, [346, [347, [348, [349, [350, [351, [352, [353, [354, [355, [356, [357, [358, [359, [360, [361, [362, [363, [364, [365, [366, [367, [368, [369, [370, [371, [372, [373, [374, [375, [376, [377, [378, [379, [380, [381, [382, [383, [384, [385, [386, [387, [388, [389, [390, [391, [392, [393, [394, [395, [396, [397, [398, [399, [400, [401, [402, [403, [404, [405, [406, [407, [408, [409, [410, [411, [412, [413, [414, [415, [416, [417, [418, [419, [420, [421, [422, [423, [424, [425, [426, [427, [428, [429, [430, [431, [432, [433, [434, [435, [436, [437, [438, [439, [440, [441, [442, [443, [444, [445, [446, [447, [448, [449, [450, [451, [452, [453, [454, [455, [456, [457, [458, [459, [460, [461, [462, [463, [464, [465, [466, [467, [468, [469, [470, [471, [472, [473, [474, [475, [476, [477, [478, [479, [480, [481, [482, [483, [484, [485, [486, [487, [488, [489, [490, [491, [492, [493, [494, [495, [496, [497, [498, [499, [500, [501, [502, [503, [504, [505, [506, [507, [508, [509, [510, [511, [512, [513, [514, [515, [516, [517, [518, [519, [520, [521, [522, [523, [524, [525, [526, [527, [528, [529, [530, [531, [532, [533, [534, [535, [536, [537, [538, [539, [540, [541, [542, [543, [544, [545, [546, [547, [548, [549, [550, [551, [552, [553, [554, [555, [556, [557, [558, [559, [560, [561, [562, [563, [564, [565, [566, [567, [568, [569, [570, [571, [572, [573, [574, [575, [576, [577, [578, [579, [580, [581, [582, [583, [584, [585, [586, [587, [588, [589, [590, [591, [592, [593, [594, [595, [596, [597, [598, [599, [600, [601, [602, [603, [604, [605, [606, [607, [608, [609, [610, [611, [612, [613, [614, [615, [616, [617, [618, [619, [620, [621, [622, [623, [624, [625, [626, [627, [628, [629, [630, [631, [632, [633, [634, [635, [636, [637, [638, [639, [640, [641, [642, [643, [644, [645, [646, [647, [648, [649, [650, [651, [652, [653, [654, [655, [656, [657, [658, [659, [660, [661, [662, [663, [664, [665, [666, [667, [668, [669, [670, [671, [672, [673, [674, [675, [676, [677, [678, [679, [680, [681, [682, [683, [684, [685, [686, [687, [688, [689, [690, [691, [692, [693, [694, [695, [696, [697, [698, [699, [700, [701, [702, [703, [704, [705, [706, [707, [708, [709, [710, [711, [712, [713, [714, [715, [716, [717, [718, [719, [720, [721, [722, [723, [724, [725, [726, [727, [728, [729, [730, [731, [732, [733, [734, [735, [736, [737, [738, [739, [740, [741, [742, [743, [744, [745, [746, [747, [748, [749, [750, [751, [752, [753, [754, [755, [756, [757, [758, [759, [760, [761, [762, [763, [764, [765, [766, [767, [768, [769, [770, [771, [772, [773, [774, [775, [776, [777, [778, [779, [780, [781, [782, [783, [784, [785, [786, [787, [788, [789, [790, [791, [792, [793, [794, [795, [796, [797, [798, [799, [800, [801, [802, [803, [804, [805, [806, [807, [808, [809, [810, [811, [812, [813, [814, [815, [816, [817, [818, [819, [820, [821, [822, [823, [824, [825, [826, [827, [828, [829, [830, [831, [832, [833, [834, [835, [836, [837, [838, [839, [840, [841, [842, [843, [844, [845, [846, [847, [848, [849, [850, [851, [852, [853, [854, [855, [856, [857, [858, [859, [860, [861, [862, [863, [864, [865, [866, [867, [868, [869, [870, [871, [872, [873, [874, [875, [876, [877, [878, [879, [880, [881, [882, [883, [884, [885, [886, [887, [888, [889, [890, [891, [892, [893, [894, [895, [896, [897, [898, [899, [900, [901, [902, [903, [904, [905, [906, [907, [908, [909, [910, [911, [912, [913, [914, [915, [916, [917, [918, [919, [920, [921, [922, [923, [924, [925, [926, [927, [928, [929, [930, [931, [932, [933, [934, [935, [936, [937, [938, [939, [940, [941, [942, [943, [944, [945, [946, [947, [948, [949, [950, [951, [952, [953, [954, [955, [956, [957, [958, [959, [960, [961, [962, [963, [964, [965, [966, [967, [968, [969, [970, [971, [972, [973, [974, [975, [976, [977, [978, [979, [980, [981, [982, [983, [984, [985, [986, [987, [988, [989, [990, [991, [992, [993, [994, [995, [996, [997, [998, [999]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]  Both versions also gracefully accept negative numbers p r[-5] # => []  • Just curious, at what value does the recursive solution fail (due to memory/stack overflow)? – mbomb007 Mar 4 '15 at 19:44 • @mbomb007 On Windows 7 x64, 16Gb RAM, it works on 926 and fails at 927 (stack level too deep (SystemStackError)) – Devon Parsons Mar 4 '15 at 19:48 PHP 5.4 (67 bytes): I know, I know. It's far from being the shortest answer. But it works! Here it is: function F($n){for($c=$n?[--$n]:[];~$n&&$n--;$c=[$n,$c]);return\$c;}


You can test it right here: https://ideone.com/42L35E (ignore the error)

Javascript (57 bytes):

This is the same exact code except that Javascript is picky about the return and I've reduced the variable names:

function F(n){for(c=n?[--n]:[];~n&&n--;c=[n,c]);return c}


See? Same code!

ES6 (49 bytes):

Basically the same exact code, but reduced for ES6:

F=n=>{for(c=n?[--n]:[];~n&&n--;c=[n,c]);return c}

• I specified no anonymous functions, or it was in the comments somewhere. I'll make it clearer. – mbomb007 Mar 5 '15 at 18:05
• @mbomb007 It wasn't specified. There was nothing enforcing a name on the function. But I've changed it. – Ismael Miguel Mar 5 '15 at 18:09
• There was a comment by me below the question: "That's mostly to prevent people from writing unassigned anonymous functions. Personally, I'd prefer it if it was a function that takes a parameter." – mbomb007 Mar 5 '15 at 18:10
• And I did change the question. But it's pretty standard codegolf for functions that they have to be callable by name (aka, more than once and without typing the entire function again.) That's why you see everyone else's functions using f=lambda... – mbomb007 Mar 5 '15 at 18:11
• @mbomb007 But it's pretty standard codegolf for functions that they have to be callable by name (aka, more than once and without typing the entire function again.) --> never heard of this, and I use this website for somewhat close to a year. Besides, this is an invalid argument since you can assign the functions to a variable. – Ismael Miguel Mar 5 '15 at 18:13

Javascript (114 bytes):

Everybody else was doing recursive, so I wanted to try an iterative solution. I have too many special cases, though.

I hold a master list, and then loop and append new lists with new numbers.

function q(n){a=[];if(n==1)a=[0];else if(n!=0){a=[0,b=[]];for(i=1;i<n;){c=[];b.push(c);b.push(i++);b=c}}return a}


Common Lisp (95 bytes):

(defun f(n &optional(i 0))(cond((< i(1- n))(cons i(list(f n(1+ i)))))((> n 0)(list i))(t nil)))


JavaScript, 35 37 bytes

Recursive solution

f=(n,x=[])=>n?f(--n,x[0]?[n,x]:[n]):x


Try it online!

05AB1E, 11 bytes

_i¯ëFNI<α)R


11-bytes alternative:

_i¯ëÝ¨Rvy)R


Explanation:

05AB1E has no loops that go downwards, so to loop in the range (input, 0] I either have to:

• Create that range first (Ý¨R; create range [0, input], remove last item, reverse), and then loop over it (vy);
• Or loop in the range [0, input) instead (F), and take the absolute difference between the loop-index and the input-1 (NI<α).
_i          # If the (implicit) input is 0:
¯         #  Push the global array (empty by default)
ë          # Else:
F         #  Loop N in the range [0, (implicit) input):
N        #   Push index N
I<      #   Push input-1
α     #   Take the absolute difference between the two
)    #   Wrap everything on the stack into a list
R   #   Reverse the list
# (output the result implicitly after the if/loop)