Erlang (programming language)/Tutorials/Lists: Difference between revisions
imported>Eric Evers |
imported>Tom Morris m (Erlang programming language/Tutorials/Lists moved to Erlang (programming language)/Tutorials/Lists) |
||
(6 intermediate revisions by one other user not shown) | |||
Line 4: | Line 4: | ||
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. | ||
==Append== | |||
append([a,b],[c,d]). | |||
[a,b,c,d] | |||
==Filter== | ==Filter== | ||
Line 19: | Line 23: | ||
120 | 120 | ||
== | ==Foldr, fold right== | ||
3> lists:foldr( fun(X, Prod)-> X*Prod end, 1, [1,2,3,4,5]). | 3> lists:foldr( fun(X, Prod)-> X*Prod end, 1, [1,2,3,4,5]). | ||
120 | 120 | ||
==Foreach(Fun, List)== | |||
41> lists:foreach( fun(X) -> X*X end, [1,2,3,4,5]). | |||
> | |||
No answer is returned because the purpose is to run side effects. | |||
42> lists:foreach( fun(X) -> io:format("~w ",X) end, [1,2,3,4,5]). | |||
1 2 3 4 5 | |||
==Last== | |||
lists:last([1,2,3]). | |||
3 | |||
==Map== | ==Map== | ||
Line 31: | Line 48: | ||
==Mapfold(left)== | ==Mapfold(left)== | ||
mapfoldl(fun(X,Prod)->{X,X*Prod} end, 1, [1,2,3,4,5]). | lists:mapfoldl(fun(X,Prod)->{X,X*Prod} end, 1, [1,2,3,4,5]). | ||
{[1,2,3,4,5], 120} | {[1,2,3,4,5], 120} | ||
==Mapfold(right)== | ==Mapfold(right)== | ||
mapfoldr(fun(X,Prod)->{X,X*Prod} end, 1, [1,2,3,4,5]). | lists:mapfoldr(fun(X,Prod)->{X,X*Prod} end, 1, [1,2,3,4,5]). | ||
{[1,2,3,4,5], 120} | {[1,2,3,4,5], 120} | ||
==Nth== | ==Nth== | ||
nth(1,[a,b,c]). | lists:nth(1,[a,b,c]). | ||
a | a | ||
==Nthtail== | ==Nthtail== | ||
nthtail(2,[a,b,c,d]). | lists:nthtail(2,[a,b,c,d]). | ||
[c,d] | [c,d] | ||
==Sort== | ==Sort== | ||
Note: sort will work with any combination of types. Each type has a natural sort order. | |||
lists:sort([2,4,3,5]). | lists:sort([2,4,3,5]). | ||
[2,3,4,5] | [2,3,4,5] | ||
lists:sort([1, 2, b, a, self(), fun()->true]). | |||
[1, 2, a, b, #Fun<erl.20.117942222>, <0,176,0>] |
Latest revision as of 06:07, 8 August 2009
The metadata subpage is missing. You can start it via filling in this form or by following the instructions that come up after clicking on the [show] link to the right. | |||
---|---|---|---|
|
Lists Module
Lists provides list functions. There is a non-standard parallel version available called plists.
Append
append([a,b],[c,d]). [a,b,c,d]
Filter
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]]
Flatten
lists:flatten([[1,2],[3,4]]). [1,2,3,4]
Fold(left)
3> lists:foldl( fun(X, Prod)-> X*Prod end, 1, [1,2,3,4,5]). 120
Foldr, fold right
3> lists:foldr( fun(X, Prod)-> X*Prod end, 1, [1,2,3,4,5]). 120
Foreach(Fun, List)
41> lists:foreach( fun(X) -> X*X end, [1,2,3,4,5]). > No answer is returned because the purpose is to run side effects.
42> lists:foreach( fun(X) -> io:format("~w ",X) end, [1,2,3,4,5]). 1 2 3 4 5
Last
lists:last([1,2,3]). 3
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]
Mapfold(left)
lists:mapfoldl(fun(X,Prod)->{X,X*Prod} end, 1, [1,2,3,4,5]). {[1,2,3,4,5], 120}
Mapfold(right)
lists:mapfoldr(fun(X,Prod)->{X,X*Prod} end, 1, [1,2,3,4,5]). {[1,2,3,4,5], 120}
Nth
lists:nth(1,[a,b,c]). a
Nthtail
lists:nthtail(2,[a,b,c,d]). [c,d]
Sort
Note: sort will work with any combination of types. Each type has a natural sort order.
lists:sort([2,4,3,5]). [2,3,4,5]
lists:sort([1, 2, b, a, self(), fun()->true]). [1, 2, a, b, #Fun<erl.20.117942222>, <0,176,0>]