Flattening a List with Many Levels

编程
0 字 / 约 0 分钟
2023/3/25
cadence
procedure ( flatten ( numberList )
	foreach ( mapcan element numberList
		if ( listp ( element )
			flatten ( copy ( element )) ;;then
			ncons ( element ))
		); if
	); foreach
); procedure

x = '((1) ((2 (3) 4 ((5)) () 6) ((7 8 ()))) 9)
flatten(x) => (1 2 3 4 5 6 7 8 9)
x =>((1) ((2 (3) 4 ((5)) nil 6) ((7 8 nil))) 9)