Luiz Miranda
Luiz Miranda

Reputation: 128

How to sum two lists together?

I'm trying to do a sum of two list, for example:

1> example:sum([4,5], [6,7])
[10,12]

Upvotes: 1

Views: 574

Answers (2)

bxdoan
bxdoan

Reputation: 1369

The Built-In Function zipwith/3 in lists module can solve your problem

> lists:zipwith(fun(X, Y) -> X+Y end, [4, 5], [6, 7]).
 [10, 12]

Upvotes: 3

stevelove
stevelove

Reputation: 3202

I like the answer @doan-bui provided. It could also be solved using zip/2 and a list comprehension.

> [X+Y || {X,Y} <- lists:zip([4, 5], [6, 7]).
[10, 12]

Upvotes: 1

Related Questions