Erlang Programming/Using lists

From Wikibooks, open books for an open world
< Erlang Programming
Jump to: navigation, search

Contents

[edit] Using lists

[edit] foreach

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.

[edit] sequence of numbers

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

5> lists:seq(1,10).
[1,2,3,4,5,6,7,8,9,10]

[edit] sort

lists:sort( A ) is what you think.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export