[erlang-questions] How to keep adding items to a data structure

Richard A. O'Keefe ok@REDACTED
Tue Apr 26 05:34:55 CEST 2016



On 25/04/16 8:05 AM, Donald Steven wrote:
> Hi Antonios,
>
> You are kind and generous to help.  I'll study this carefully.  I will 
> be wanting to add a list or a record, not just numbers.  Is this 
> possible?
There is no restriction on what can be in a list, unless you add such a 
restriction yourself.

Good list processing in functional languages is like good array processing
in Fortran, APL, or R:  *don't* think element-at-a-time, think 
whole-collection.
In Fortran, you can add two arrays two ways:
    do I = 1, N
      A(I) = B(I) + C(I)
    end do
element-at-a-time or
    A = B + C
whole-collection.

In Erlang, you *can* write a recursive function to generate the elements
of a list, but most of the time it's less work and more readable to use
a higher order function from the lists module.  For example, to add
corresponding elements of two lists, you could write

add([X|Xs], [Y|Ys]) -> [X+Y | add(Xs, Ys)];
add([],     []    ) -> [].

    ... A = add(B, C) ...

but it's less work to write

    ... A = lists:zipwith(fun (X,Y) -> X+Y end, B, C) ...

where you focus on saying *what* is to be done with corresponding
elements instead of *how to find them and construct a result*.

EXAGGERATION WARNING: the next sentence is an exaggeration.

   If you have two loops with the same structure in a functional
   language, you are doing it wrong, because you should put name
   the shared control structure and write it just once and then
   use it twice.

EXAGGERATION WARNING: the previous sentence was an exaggeration.
But not *much* of an exaggeration.

Two more pieces of advice:

(1) Look up "Learn You Some Erlang for Great Good!"
     http://learnyousomeerlang.com
     When you have read it, buy a copy of the book to thank
     Fred Hébert for his help, and read it again a couple of
     times.

(2) Erlang/OTP 18.2 came with 4057 *.erl files in lib/.  You
     are never going to read all of them.  But there are some
     that you are going to use over and over again.  You
     should at least read their documentation.  If there is a
     module mentioned in Learn You Some Erlang, look it up at
     http://erlang.org/erldoc
     You should find out what is there in the 'erlang' module
     and what modules are in 'stdlib' especially the 'lists'
     module.   You won't stand a chance of remembering every-
     thing you see, but do get a feel for what kind of stuff
     is there and where you might look.

Did I mentioned Learn You Some Erlang?  There are other great
books about Erlang, but that's on-line.

> Don
>
> On 04/24/2016 03:13 PM, Antonios Kouzoupis wrote:
>> Hi Don,
>>
>> The way you iterate in Erlang and I guess in most functional programming
>> languages is by recursive call. So if you want to add/append some
>> numbers to a list, one way to go is the following:
>>
>> populate(Num) ->
>>      populate(Num, []).
>>
>> populate(0, Acc) ->
>>      Acc;
>> populate(Num, Acc) ->
>>      populate(Num - 1, [Num | Acc]).
>>
>>
>> Now if you call populate(100), you'll get the list [1,...,100]
>>
>> BR,
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list