当前位置:网站首页>Erlang learning 02
Erlang learning 02
2022-07-24 10:17:00 【Tsuit】
fun
fun: Basic Abstract units , stay Erlang The data type used to represent functions in is called fun.
Standard library lists The module exports some with fun As a function of parameters . The most useful of them is lists:map(F, L). This function returns a list , It gives the list L Each element in the application fun F Generate .
Another useful function is lists:filter(P, L), It returns a new list , contains L All qualified elements in
9> L=[1,2,3,4].
[1,2,3,4]
10> lists:map(fun(X) -> 2*X end,L).
[2,4,6,8]11> Even=fun(X) ->(X rem 2) =!= 0 end.
* 1: syntax error before: '!'
11> Even=fun(X) ->(X rem 2) =:= 0 end.
#Fun<erl_eval.6.118419387>
12> Even(3).
false
13> Even(2).
true
14> lists:map(Even,[1,2,3,4,5,6,8]).
[false,true,false,true,false,true,true]
15> lists:filter(Even,[1,2,3,4,5,6,8]).
[2,4,6,8]
map and filter Such functions can perform some operation on the whole list in one call , We call them one list at a time (list-at-a-time) Type operation . Use one list operation at a time to make the program smaller , And it's easy to understand . The reason why it is easy to manage The solution is that we can regard each operation on the whole list as a single conceptual step of the program . otherwise , You must align the columns Each operation of the table element is regarded as a separate step of the program .
Functions can not only be used fun As a parameter ( for example map and filter), You can go back to fun.
1> Fruit=[apple,pear,orange].
[apple,pear,orange]
2> MakeTest=fun(L) -> (fun(X)-> lists:member(X,L) end) end.
#Fun<erl_eval.6.118419387>
3> IsFruit=MakeTest(Fruit).
#Fun<erl_eval.6.118419387>
4> IsFruit(pear).
true5> lists:filter(IsFruit,[dog,orange,cat,pear,apple]).
[orange,pear,apple]
-module(shop2).
-export([total/1]).
-import(lists,[map/2,sum/1]).
total(L) ->
sum(map(fun({What,N}) -> shop:cost(What)*N end,L)).11> c(shop2).
{ok,shop2}
12> Buy=[{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}].
[{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}]
13> L1=lists:map(fun({What,N}) -> shop:cost(What)*N end,Buy).
[20,8,20,54,21]
14> lists:sum(L1).
123
You should also pay attention to the module -import and -export Usage of declarations . -import(lists, [map/2, sum/1]). The statement means map/2 Function is from lists Imported from the module , The back is the same . That means we can use map(Fun, ...) Instead of lists:map(Fun, ...) It's written in .cost/1 Not declared in the import declaration , So you have to use “ Completely limited ”(fully qualified) The name of shop:cost.
-export([total/1]) The statement means total/1 The function can be in shop2 Call outside the module . Only from one Only functions exported from modules can be called outside the module .
The list of deduction
The list of deduction (list comprehension) No need to use fun、map or filter You can create the expression of the list . It makes the program shorter , Easier to understand .
Use map
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]Use list derivation
3> [2*X || X <- L].
[2,4,6,8,10]
[ F(X) || X <- L] The mark means “ from F(X) A list of components (X From the list L Extract from )”. therefore ,[2*X || X <- L ] It means “ from 2*X A list of components (X From the list L Extract from )”.
边栏推荐
- SMTP automatic mail sending function code
- CAS principle [concurrent programming]
- System a uses window.open to call system B, and system B carries data back to system a after processing the business
- Redis configuration serialization
- String__
- Erlang学习02
- 2022, our small goal
- Google Earth engine - QA in Landsat 5 toa dataset_ Pixel and QA_ Radsat band
- Source insight 3.5 comment garbled
- 第五章 修改实现(IMPL)类
猜你喜欢

2022, enterprise unified process platform design and integration specifications refer to thubierv0.1

二叉树、二叉树排序树的实现及遍历
![[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus](/img/8f/19b0eb959d2b3f896c8e99f8e673d1.png)
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus

Jenkins deploys the project and prompts that the module package defined by him cannot be found

Analysis of distributed lock redistribution principle

Query about operating system security patch information
![[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display](/img/f2/6fcd4b2e747b4ceb52a52eda0c1af4.png)
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display

When the hot tea is out of stock, what does the new tea drink rely on to continue its life?

Interpretation of websocket protocol -rfc6455

Dr. water 3
随机推荐
Installation UMI tutorial (error reporting and solutions)
Erlang学习01
JS bind simulation
Arduino- use millis() to do two (or more) things at the same time
聚集日志服务器
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus
In the envy of LETV, there is a "meaning crisis" of contemporary workers
The heads of the five major international institutions called for urgent action to deal with the global food security crisis
Uniapp calendar component
Raspberry Pie: serial port login does not display print information
Arduino serial port information reading and output
Where is the bitbucket clone address
Trie tree template 2
Balance between management / business and technology
Websocket 协议解读-RFC6455
Arduino drive lcd1602a
Dynamic programming -- a collection of stock problems
[leecode] get the longest common substring of two strings
zoj-Swordfish-2022-5-6