[erlang-questions] concentate solution well ??

Richard A. O'Keefe ok@REDACTED
Mon Feb 2 03:09:32 CET 2015


On 1/02/2015, at 11:14 pm, Roelof Wobben <r.wobben@REDACTED> wrote:

> Hello,
> 
> I have this exercise of the Erlang programming book.
> 
> Write a function that, given a list of lists, will concatenate them.
> Example:
> concatenate([[1,2,3], [], [4, five]]) ⇒ [1,2,3,4,five].
> Hint: you will have to use a helpfunction and concatenate the lists in several steps.
> 
> But I do not need to use a helper function because it can be solved like this :
> 
> concatenate( [List1 , List2 , List3] ) ->
>    List1 ++ List2 ++ List3.

OK, so that works for a list of THREE lists.

What are you going to do if the list has THIRTY THOUSAND elements?
Or if it might be *ANY* number of elements -- which is the usual point
of having a list.

You will not in fact need a helper function;
you can use the built-in ++ operator.

So, using Haskell syntax to give you *something* to do:

concatenate [] = ?what?
concatenate (list : list_of_lists) =
  ?how will you concatenate the list_of_lists?
  ?how will you combine the result of that with the first list?

Given the pieces

    list : list(T)
    list_of_lists : list(list(T))
    concatenate (list(list(T)) -> list(T))
    ++ : list(T) , list(T) -> list(T)

there is only one way to put them together that makes sense,
so this should not be hard.





More information about the erlang-questions mailing list