flatmap

Robert Virding robert.virding@REDACTED
Mon Feb 27 14:17:32 CET 2006


No, it should say append! Flatten/1 works recursively to completely 
flatten an term while append/1 only does it at the top level.

Flatmap/2 should only flatten the top level, each call to Function 
returns a list of elements and flatmap doesn't return a list of lists, 
just a list of elements. If that makes sense? :-)

So while

map(F,L) -> [[1,2,3],[4,5,6],[7,8,9]]

then

flatmap(F,L) -> [1,2,3,4,5,6,7,8,9]

If I remember correctly.

The types don't really say much though, perhaps they should be:

Function = fun (A) -> [B]
List1 = [A]
Element = [B]

/Robert

Mats Cronqvist skrev:
>   happended upon the description of lists:flatmap/2 (that i've never 
> used), and was a bit mystified by the append/1 function used in the 
> description. could it be that it should say flatten/1 ?
> 
>   mats
> 
> flatmap(Function, List1) -> Element
> Types:
> Function = fun(A) -> B
> List1 = [A]
> Element = [B]
> 
> flatmap behaves as if it had been defined as follows:
> 
> flatmap(Func, List) ->
>     append(map(Func, List))
> 



More information about the erlang-questions mailing list