Head and tail
[head | tail ] = [1] #head 1 tail [] [head | tail ] = [1, 2, 3] #head 1 tail [2, 3] [head | tail ] = [] #报错
Create a mapping function
We can use a function to process each element in the list, so you can accept the more complex processing, it can be processed differently according to the function passed into the function.
def map([], _func), do: [] def map([ head | tail ], func), do: [func.(head) | map(tail, func)] Example.map [1,2,3,4], fn n -> n * n end #[1, 4, 9, 16]
Tracking value in a recursive process
Our goal is to use immutable state, it can not be a global variable or module-level variable value stored embodiment. So, we pass parameters to a function
SUM DEF ([], Total), do: Total DEF SUM ([head | tail], Total), do SUM (tail, Total + head) Example.sum ([ 1,2,3,4], 0) # 10 # we always have to pass an initial value can be improved as follows DEF SUM (List), do: SUM (List, 0 ) DEFp _sum appended to ([], total), do: total DEFp _sum appended to ([head | tail], total ), do: sum (tail, total + head)
Use the function to solve the problem
DEF the reduce ([], value, _), do: value DEF the reduce ([head | tail], value, FUNC), do: When reduce (. tail, func (head , value), func) # use anonymous functions in plus a point before the argument list (.)
Example.reduce
More complex lists
# Exchanged close two data, if an odd number of data is given DEF the swap ([]), do: [] DEF the swap ([A, B | tail]), do: [B, A | the swap (tail)] DEF the swap ([_]), do: The raise "the swap Can`t A Number of List Elements with AN ODD"
Can be used [a, ..., x | tail] match a set of data
# [Timestamp, location_id, temperature, rainfall] This data set represents Weather # version of a DEF for_location_27 ([]), do: [] DEF for_location_27 ([[Time, 27, the TEMP, Rain] | tail]) do
[[Time , 27, TEMP, Rain] | for_location_27 (tail)] # location_id screened a set of data of 27
End DEF for_location_27 ([_ | tail]), do: for_location_27 (tail) # skip a set of data format mismatch in a
# version two
# more incoming data to filter
DEF for_location ([], _target_loc), do: []
DEF for_location ([[Time, target_loc, the TEMP, Rain] | tail], target_loc) do
[[Time , target_loc, the TEMP, Rain] | for_location (tail, target_loc)]
End
DEF for_location ([_ | tail], target_loc), do: for_location (tail, target_loc)
# Third version
# The matching function simplifies to:
DEF for_location (head = [_, target_loc, _, _] | tail], target_loc), do: [head | for_location (tail, target_loc)]
List module provides functions
connection. [1, 2, 3] + [4, 5, 6]
One-dimensional. List.flatten ([[[1], 2], [[[3]]]]) => [1, 2, 3]
折叠。List.foldl([1, 2, 3], "", fn value, acc -> "#{value}(#{acc})" end ) =>3(2(1()))
List.foldr([1, 2, 3], "", fn value, acc -> "#{value}(#{acc})" end ) =>1(2(3()))
合并、拆分。l = List.zip([ [1, 2, 3], [:a, :b, :c], ["cat", "dog"] ] ) =>[ {1, :a, "cat"}, {2, :b, "dog"}]
List.unzip( l ) => [ [ 1, 2 ], [ :a, :b ], [ "cat", "dog" ]
Access tuples in the list. kw = [{: name, "Dave"}, {: likes, "Programmin"}, {: where, "Dallas", "TX"}]
List.keyfind (kw,: name, 0) {: name, "Dave"} Parameters: list, tuple of data values, the numerical subscript in the tuple
List.keyfind(kw, "TX", 2) {:where, "Dallas", "TX"}
List.keyfind(kw, "TX", 1) nil
Delete tuples. List.keydelete (kw, "TX", 2)
Replace the tuple. List.keyreplace (kw,: name, 0, {: first_name, "Dave"})