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


 
\ take a list (array) and flatten it:
 
: (flatten)  \ a -- a
	(
		\ is it a number?
		dup >kind ns:n n:= if
			\ yes.  so add to the list
			r> swap a:push >r
		else
			\ it is not, so flatten it
			(flatten)
		then
		drop
	) a:each drop ;
 
: flatten \ a -- a
	[] >r (flatten) r> ;
 
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
dup . cr
flatten 
. cr 
bye
 