Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through all elements in a list with Erlang

I'm new to Erlang. All I want to do is take a list, loop through each element so I can send them to a function. Cannot find a clear example anywhere.

Example of what I want to do:

Mylist = [a,b,c,d,e,f,g]

for (i in Mylist) {
  otherFunction(Mylist[i]);
}
like image 361
user9667428 Avatar asked Apr 19 '18 03:04

user9667428


1 Answers

Hope that help :)

func([]) -> ok;
func([H|T]) ->
    otherFunction(H),
    func(T).

Or you can use list comprehension:

[otherFunction(H) || H <- L].
like image 146
bxdoan Avatar answered Oct 02 '22 23:10

bxdoan