Task

Write a function to flatten the nesting in an arbitrary list of values.

Your program should work on the equivalent of this list:

  [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]

Where the correct result would be the list:

   [1, 2, 3, 4, 5, 6, 7, 8]

Related task

   Tree traversal


include FMS-SI.f
include FMS-SILib.f
 
: flatten {: list1 list2 --  :}
  list1 size: 0 ?do i list1 at: 
                  dup is-a object-list2
                  if list2 recurse else list2 add: then  loop ;
 
object-list2 list 
o{ o{ 1 } 2 o{ o{ 3 4 } 5 } o{ o{ o{ } } } o{ o{ o{ 6 } } } 7 8 o{ } } 
list flatten
list p: \ o{ 1 2 3 4 5 6 7 8 } ok