Flattening a List with Many Levels

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)

发表评论