News for hackers. Stuff that matters.
Re:Why it’s hard for programmers to write a program to flatt... by ng12 on Friday February 24, 2017 @02:15PM (#13726711)
I would either use Object to be quick, or implement a NestedListItem with isNumber(), getNumber(), getList().

The one thing I do like about this problem is it shows when TDD is useful. If you try to write a test case first the representation problem will be the very first thing you run into, and the code will mostly follow from there.

Re:Why it’s hard for programmers to write a program to flatt... by wz1000 on Friday February 24, 2017 @02:28PM (#13726845)
Personally, I think this problem shows why sane type systems are useful.

In Haskell

    data NestedList a = Single a | Nested [NestedList a]
    -- Or equivalently, type NestedList = Free []

    flatten :: NestedList a -> [a]
    -- This is pretty much the only reasonable definition of flatten the compiler will accept given this type
    flatten (Single a) = [a]
    flatten (Nested xs) = concatMap flatten xs

    -- or given the Free variation of NestedList
    -- flatten = retract
Re:Why it’s hard for programmers to write a program to flatt... by dllthomas on Friday February 24, 2017 @02:30PM (#13726862)
Not necessarily. See my bash solution in another comment :-D