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


 
1) Let's create this function
 
{def A.flatten
 {def A.flatten.r
  {lambda {:a}
   {if {A.empty? :a}
    then 
    else {let { {:b {A.first :a}}
              } {if {A.array? :b}
                 then {A.flatten.r :b}
                 else :b} }
         {A.flatten.r {A.rest :a}} }}}
 {lambda {:a}
  {A.new {A.flatten.r :a}}}}
-> A.flatten
 
and test it
 
{def list
 {A.new
  {A.new 1}
  2
  {A.new {A.new 3 4} 5}
  {A.new {A.new {A.new }}}
  {A.new {A.new {A.new 6}}}
  7
  8
  {A.new}
}}
->  [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
 
{A.flatten {list}}
-> [1,2,3,4,5,6,7,8]
 