In this challenge, you must parse a list of lists, into a simpler list format.

This challenge is based on my sadflak parser. In my sadflak parser, it has all the () removed, replaced with the sum of the ()s at the start of the list, to make the program run faster.

To parse into a Sad-List, you have to do this (python implementation thing, uses a tuple of tuples):

def sadlistfunc(list):
    new-sadlist = [0]
    for i in list:
        if i == ():

This is a recursive function. For a list, start a new list, starting with the number of () from the list input, then the rest of this list is sad-list versions of every list that was not a () from the list input, in order. return the list.


you may take input in a few different formats:

  • you may take it as a list
  • you may take it as a tuple
  • you may take it as a string

if you take it as a string, you should use some set of brackets, as appear in brain-flak. you may not use characters 1 and 2

just be reasonable

Input will always be inside one list, but your program may assume an implicit list layer outside of the input, i.e. ()()() = (()()()), or it may choose not to. Examples will be with explicit outside list


may be list or tuple or string, or whatever. you may use whatever reasonable output format, as is the meta consensus.


(()()()) = [3]
(((()))) = [0,[0,[1]]]
((())()(())) = [1, [1], [1]]
() = invalid input, if the outside bracket is explicit.
((((())())())(())()) = [1, [1, [1, [1]]], [1]]

note that input is not strict. these inputs could be:


or some other reasonable format

explained test case:


to "sadify" this, first we count the number of ()

 ()()        ()
(    ((())())  )

3. then we remove these, and add a 3 at the start


there is one list in this list. we sadify this


how many ()?

((())  )

1. we remove and add a 1 at the start


this has one list in it



(  )

remove and add count


then we put this back into its list


then we put this back into its list



This is , so shorter is better

  • \$\begingroup\$ Note that in the actual sad-flak parser, the number of () is actually the second item of the list, and the first item is the index of the command \$\endgroup\$ Jun 26, 2017 at 0:45
  • \$\begingroup\$ Good ol' JavaScript for... in, making me remember why you never use it: Fiddle \$\endgroup\$
    – Stephen
    Jun 26, 2017 at 1:17
  • \$\begingroup\$ I suppose ((((())())())(())()) = [1, [1, [1, [1]], [1]] should be ((((())())())(())()) = [1, [1, [1, [1]]], [1]]. \$\endgroup\$
    – Renzo
    Jun 26, 2017 at 8:19

11 Answers 11


Pyth, 13 bytes


Test suite.

How it works

L               define a function y with argument b:
   /bY              list 1: count how many [] can be found in b
  ]                         wrap into singleton
        -b]Y        list 2: filter out [] from b
      yM                    apply y (this function) to each
 +                  concatenate the two lists above
            y   apply y to the input
  • \$\begingroup\$ You can remove first ]. \$\endgroup\$ Jun 26, 2017 at 8:24

CommonLisp, 49 bytes

(defun s(l)(cons(count()l)(mapcar's(remove()l))))

take input as list of lists.

Try it online!


Brachylog, 21 bytes


Try it online!

  • \$\begingroup\$ Bytes and characters are not equivalent. Using UTF-8, that would take up 33 bytes, despite being only 21 characters. \$\endgroup\$
    – squirl
    Jun 26, 2017 at 18:41
  • 1
    \$\begingroup\$ @Samadi Brachylog uses its own code page, which is allowed per this meta answer. \$\endgroup\$
    – Leaky Nun
    Jun 27, 2017 at 1:07
  • \$\begingroup\$ Ah, I see. I was slightly confused. Thanks for clarifying! \$\endgroup\$
    – squirl
    Jun 27, 2017 at 7:42
  • \$\begingroup\$ @Samadi No problem, this question gets asked all the time. \$\endgroup\$
    – Leaky Nun
    Jun 27, 2017 at 7:43

Mathematica, 42 bytes


Avoids explicit recursion by using //@ (MapAll) which maps a function over every node in a tree. This also means that the functions are executed from the leaves upwards. However, it will also be applied to {} which get turned into {0} in the process. That's why we count and remove {0} instead {}.


Retina, 42 bytes


Try it online!


Clojure, 59 bytes

(fn f[i](conj(map f(remove #{[]}i))(count(filter #{[]}i))))

Not much different from the CommonLisp answer. Its count and remove seem to accept a bit nicer construct, here I had to use sets.


Actually, 12 bytes


Try it online!

Takes input as a comma-separated square-bracket list with explicit outer brackers.


;[]@c         count the number of empty lists
     @;░      filter out empty lists
        Q£Mo  recurse with filtered list and append result

Python 2, 69 46 45 bytes

f=lambda l:[l.count([])]+map(f,filter(len,l))

Try it online!

  • \$\begingroup\$ I think you need to add f= to your bytecount, since you're using the function f, and naming it otherwise would break your solution \$\endgroup\$
    – Leo
    Jun 27, 2017 at 9:56
  • \$\begingroup\$ @Leo You are right. \$\endgroup\$
    – ovs
    Jun 27, 2017 at 10:10

Jelly, 10 bytes


Try it online!

Takes input as list of lists of lists...

Of course it uses the algorithm the other answers use. ;)

  • \$\begingroup\$ That's not 10 bytes. It's 10 characters. The number of bytes will depend on what encoding you use. \$\endgroup\$
    – squirl
    Jun 26, 2017 at 18:40
  • 2
    \$\begingroup\$ @Samadi No, Jelly has a dedicated character set which is its default, and it can represent those characters as one byte each. See here. \$\endgroup\$
    – Adám
    Jun 26, 2017 at 23:46
  • \$\begingroup\$ I see. Thanks for the clarification! \$\endgroup\$
    – squirl
    Jun 27, 2017 at 7:40

Haskell, 102 bytes

data L=I Int|T[L]deriving Show
(I n:r)#m=r#(n+m)
_#m=[I m]
f(T[])=I 1
f(T t)=T$map f t#0

Try it online!

Because Haskell is strictly typed, there are no arbitrarily nested lists. As a remedy data L=I Int|T[L]deriving Show declares tree-like nested lists with either Ints or empty lists as leafs.

Input is like in the second example format, with an additional constructor T before each opening brace: T[T[T[]],T[],T[T[]]]. The same goes for the output, with each number being preceded by a constructor I. Function f performs the saddening.

Outputs for the test cases:

T [I 3]
T [T [T [I 1],I 0],I 0]
T [T [I 1],T [I 1],I 1]
T [T [T [T [I 1],I 1],I 1],T [I 1],I 1]

Javascript (ES6), 77 bytes


let m=a=>!a.length||a.map(m).reduce((b,c)=>(c.length?b.push(c):b[0]++,b),[0])


const traverse = arr => !arr.length || arr
        (accum, val) => (val.length ? accum.push(val) : accum[0]++, accum),


let m=a=>!a.length||a.map(m).reduce((b,c)=>(c.length?b.push(c):b[0]++,b),[0])

o.innerHTML = [
<pre id=o></pre>


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.