3 ms·
This only flattens one level. Consider the following snippets: ; CHICKEN Scheme #;1> (flatten '((1 2 3) ((4 5) 6) (7 (8) (((((9)))))))) (1 2 3 4 5
by ThatGeoGuy 10y ago
This only flattens one level. Consider the following snippets:
; CHICKEN Scheme
#;1> (flatten '((1 2 3) ((4 5) 6) (7 (8) (((((9))))))))
(1 2 3 4 5 6 7 8 9)
#;2> (apply append '((1 2 3) ((4 5) 6) (7 (8) (((((9))))))))
(1 2 3 (4 5) 6 7 (8) (((((9))))))
vs.
# Python 3
>>> sum([[1,2,3], [[4,5],6], [7, [8], [[[[[9]]]]]]], [])
[1, 2, 3, [4, 5], 6, 7, [8], [[[[[9]]]]]]
There's a big difference here. Flattening a list to just the elements inside isn't terribly hard, especially in a language like Scheme with tail-recursion, but flatten is definitely something that should be in the standard library. The "flatten" you propose is really just appending the elements of the first level of the list.
- brettcannon 10y agoSee https://bugs.python.org/issue27852 https://bugs.python.org/issue27852 for a discussion as to why there isn't a more general flatten().