Erlang (programming language)/Tutorials/Lists: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
imported>Eric Evers
Line 5: Line 5:
Lists provides list functions. There is a non-standard parallel version available called plists.
Lists provides list functions. There is a non-standard parallel version available called plists.


==Filter==
==Fold(left)==
  1> F1 = fun(Item) -> length(Item) > 1 end.  
  1> F1 = fun(Item) -> length(Item) > 1 end.  
  #Fun<erl_eval.6.58606484>
  #Fun<erl_eval.6.58606484>
  2> lists:filter(F1, [[1],[1,2],[1,2,3]]).
  2> lists:filter(F1, [[1],[1,2],[1,2,3]]).
  [[1,2,3],[1,2]]
  [[1,2,3],[1,2]]
==Filter==
3> lists:foldl( fun(X, Prod)-> X*Prod end, 1, [1,2,3,4,5]).
120


==Map==
==Map==
Line 17: Line 21:
  7> lists:map(Sqrt,[1,2,3,4]).
  7> lists:map(Sqrt,[1,2,3,4]).
  [1.00000,1.41421,1.73205,2.00000]
  [1.00000,1.41421,1.73205,2.00000]
==Nth==
nth(1,[a,b,c]).
a
==Nthtail==
nthtail(2,[a,b,c,d]).
[c,d]


==Sort==
==Sort==
  lists:sort([2,4,3,5]).
  lists:sort([2,4,3,5]).
  [2,3,4,5]
  [2,3,4,5]

Revision as of 14:45, 2 June 2008


Lists Module

Lists provides list functions. There is a non-standard parallel version available called plists.

Fold(left)

1> F1 = fun(Item) -> length(Item) > 1 end. 
#Fun<erl_eval.6.58606484>
2> lists:filter(F1, [[1],[1,2],[1,2,3]]).
[[1,2,3],[1,2]]

Filter

3> lists:foldl( fun(X, Prod)-> X*Prod end, 1, [1,2,3,4,5]).
120 

Map

6> Sqrt = fun(X) -> math:sqrt(X) end.
#Fun<erl_eval.6.56006484>
7> lists:map(Sqrt,[1,2,3,4]).
[1.00000,1.41421,1.73205,2.00000]

Nth

nth(1,[a,b,c]).
a

Nthtail

nthtail(2,[a,b,c,d]).
[c,d]

Sort

lists:sort([2,4,3,5]).
[2,3,4,5]