Erlang Programming/Using lists

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Using lists[edit]


3> lists:foreach( fun(X)->X*X end, [1,2,3]).

produces no output because the purpose of foreach is to generate side-effects. However,

4> lists:foreach( fun(X)->io:format("~w ",[X]) end, [1,2,3,4]).
1 2 3 4

does work, because io:format() is a side effect function.

sequence of numbers[edit]

lists:seq(1,100) is like range(1,101) in python.

5> lists:seq(1,10).


lists:sort( A ) is what you think.

6> lists:sort([1,3,2,6,5,4]).
7> lists:sort([a,d,b,c]).
8> lists:sort([f,e,a,"d","c",{b}]).