views:

123

answers:

4

EDIT: My question was originally "Is there a standard name for a function that flattens a list of lists, but only one level deep?", but Chuck's answer is phrased much closer to what I actually wanted to ask, so I renamed it. All three answers were useful to me, though. Thanks.

'flatten' seems to be a well-accepted name for a function that takes a tree and builds a list of atoms however deep they are nested, but what about a function that stops after just one level? So ((1 2) ((3 4) (5 6)) (7 8)) "somethings" to (1 2 (3 4) (5 6) 7 8). Does "something" have a common name across multiple languages/libraries?

The answers to this question:

http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python

suggest that 'chain' might be a good guess, but is it common enough to be "standard"?

+2  A: 

I'm not sure there is a standard name for this. I can name 3 different implementations with 3 different names

  • Python: chain
  • F#: concat
  • LINQ: SelectMany
JaredPar
+3  A: 

The function that takes a list of lists and returns a single list containing the contents of those lists is called "concat" in many functional languages (e.g. OCaml, F#, Haskell, Clojure).

Chuck
+5  A: 

For removing an inner set of brackets of a list of lists, concat is very popular. A more general function, for flattening an M of Ms for a monad M, is often called join. In abstract algebra, this function is standardly called µ.

Apocalisp
A: 

In Common Lisp, you can apply APPEND, or CONCATENATE with the right type parameter. The result of APPEND shares substructure with the argument lists; CONCATENATE always copies, and can also be applied to non-list sequences.

Svante
But don't both of those take the lists to be joined up as individual parameters, not contained within a single list? So I can do (append (1 2) ((3 4) (5 6)) (7 8)) and get back (1 2 (3 4) (5 6) 7 8), but (append ((1 2) ((3 4) (5 6)) (7 8))) would return ((1 2) ((3 4) (5 6)) (7 8)), right?
jtolle
Yes, but you can use a _spreadable argument list_ with APPLY, or use REDUCE to do it step by step.
Svante
Right. What I was asking for is what a common name might be for a function that does (apply #'append list-of-lists) or (reduce #'append list-of-lists). 'concat', 'join', or 'chain' all seem plausible.
jtolle