A second function module (lower)

Simple list processing

  On one of our focus on the fun, and now we continue to prepare sum and map, to calculate the total sum of our improvements achieved before. Create a new mylists.erl

1 -module (mylists).
2 -export ([sum/1]).
3 
4 sum([H | T])    ->    H + sum(T);
5 sum([])            ->    0.

  Then performed on the shell.

  1> L = [1,3,10].
  [1,3,10]
  2> mylists:sum(L).
  14

  Then we add a map method on the original lists.

1 -module (mylists).
2 -export ([sum/1,map/2]).
3 
4 sum([H | T])    ->    H + sum(T);
5 sum([])            ->    0.
6 
7 map(_, [])        ->    [];
8 map(F, [H | T])    ->    [F(H) | map(F, T)].

  Results are as follows:

  1> L = [1,2,3,4,5].
  [1,2,3,4,5]
  2> mylists:map(fun(X) -> 2*X end, L).
  [2,4,6,8,10]
  3> mylists:map(fun(X) -> X*X end, L).
  [1,4,9,16,25]

  Now that we understand the sum and map, we are ready to use this to write shop2.erl

1 -module (shop2).
2 -export ([total/1]).
3 -import (lists, [map/2, sum/1]).
4 
5 total(L)    ->    sum(map(fun({What, N})    ->    shop:cost(What) * N end, L)).

  Run after the results are:

  1> L = [1,2,3,4,5].
  [1,2,3,4,5]
  2> mylists:map(fun(X) -> 2*X end, L).
  [2,4,6,8,10]
  3> mylists:map(fun(X) -> X*X end, L).
  [1,4,9,16,25]

   -import and -export usage:

  -import (lists, [map / 2, sum / 1]) statement means that map / 2 introduced from the function lists module.

  -export ([total / 1]) statement mean total / 1 function can be called from outside the module shop2 only derived from a module function can be called outside of the module.

List comprehension

  Without using a list comprehension is fun, map or filter will be able to create a list of expressions which make the program shorter and easier to understand.

  Before we are using lists: map to achieve

  1> L = [1,2,3,4,5].
  [1,2,3,4,5]
  2> lists:map(fun(X) -> 2*X end, L).
  [2,4,6,8,10]

  Now we look at a list comprehension is how to achieve:

  3> [2*X || X <- L].
  [2,4,6,8,10]

  [F (X) || X <- L] mark means "a list of F (X) consisting of (X extracted from the list L)"

  Note that, the right symbol || X is a pattern, for each element of the list L matches the inside left side of 2 * X is a constructor.

  Finally, we derived the list to change the way shop2.erl

  total(L)  ->

    lists:sum([shop:cost(A)*B||{A,B} <- L]).

  The most common form of this list is derived the expression: [X || Qualifier1, Qualifier2, ...] X is an arbitrary expression, the latter may be a generator qualifier bit string generator or filter.

    Writing Pattern 1. Generator <- ListExpr, wherein ListExp must be able to draw a list of expressions.

    2. The bit string generator wording is BitStringPattern <= BitStringExpr, wherein BitStringExpr expression must be able to draw a string of bits.

    3. Analyzing the filter may either function may be a Boolean expression [X || {a, X} <-. [{A, 1}, {b, 2}, {c, 3}, {a, 4}, hello, "WoW"]]. The result is performed [1,4]

Built-in functions

  Built-in functions referred to BIF, is defined as a function erlang part of those languages.

Points

  Level is a structure that can be used to increase the use of pattern matching.

case and if the expression

  case syntax is as follows:

  case Expression of

    Pattern1 [when Guard1]  -> Expr_seq1;

    Pattern2 [when Guard2]  -> Expr_seq2;

    ......

  end

  erlang also provides a second conditional expression if:

  if

    Guard1 ->

      Expr_seq1;

    

    Guard2 ->

      Expr_seq2;

    ...

  end

Reproduced in: https: //www.cnblogs.com/malkin/p/4907402.html

Guess you like

Origin blog.csdn.net/weixin_34167043/article/details/94607734