当前位置:网站首页>Erlang学习02
Erlang学习02
2022-07-24 10:12:00 【Tsuit】
fun
fun:基本的抽象单元,在Erlang 中用于代表函数的数据类型被称为fun。
标准库里的lists模块导出了一些以fun作为参数的函数。它们之中最有用的是lists:map(F, L)。这个函数返回的是一个列表,它通过给列表L里的各个元素应用fun F生成。
另一个有用的函数是lists:filter(P, L),它返回一个新的列表,内含L中所有符合条件的元素
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和filter等函数能在一次调用里对整个列表执行某种操作,我们把它们称为一次一列表 (list-at-a-time)式操作。使用一次一列表式操作让程序变得更小,而且易于理解。之所以易于理 解是因为我们可以把对整个列表的每一次操作看作程序的单个概念性步骤。否则,就必须将对列 表元素的每一次操作视为程序的单独步骤了。
函数不仅可以使用fun作为参数(例如map和filter),还可以返回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
还应该注意模块里-import和-export声明的用法。 -import(lists, [map/2, sum/1]).声明的意思是map/2函数是从lists模块里导入的, 后面的也一样。这就意味着我们可以用map(Fun, ...)来代替lists:map(Fun, ...)的写法了。cost/1没有在导入声明中声明过,因此必须使用“完全限定”(fully qualified) 的名称shop:cost。
-export([total/1])声明的意思是total/1函数可以在shop2模块之外调用。只有从一 个模块里导出的函数才能在该模块之外调用。
列表推导
列表推导(list comprehension)是无需使用fun、map或filter就能创建列表的表达式。它让程序变得更短,更容易理解。
使用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]使用列表推导
3> [2*X || X <- L].
[2,4,6,8,10]
[ F(X) || X <- L]标记的意思是“由F(X)组成的列表(X从列表L中提取)”。因此,[2*X || X <- L ]的意思就是“由2*X组成的列表(X从列表L中提取)”。
边栏推荐
- [STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)
- Tag the specified commit and submit the tag
- JS 84*148=b6a8 how many decimal places can you make both sides equal
- How to solve command 'xxx GCC' not found, but can be installed with:??
- [STM32 learning] (4) press the key to control the flow light
- Implementation principle of acid in MySQL
- [STM32 learning] (6) use of serial port 1 (usart1)
- MySQL 数据库 JDBC编程
- How does ribbon get the default zoneawareloadbalancer?
- MySQL query database capacity size
猜你喜欢

Dynamic planning: robbing families and houses

高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力

An article takes you to understand the operation of C language files in simple terms

Websocket 协议解读-RFC6455

Curse of knowledge

Add a love power logo to your website

Dr. water 3

Detailed explanation of uninstalling MySQL completely under Linux

Spark Learning: how to choose different association forms and mechanisms?

How does ribbon get the default zoneawareloadbalancer?
随机推荐
[STM32 learning] (8) stm32f1 general timer configuration
In the envy of LETV, there is a "meaning crisis" of contemporary workers
Implementation and traversal of binary tree and binary tree sorting tree
Reading makes people improve my list
AttributeError: module ‘sipbuild. api‘ has no attribute ‘prepare_ metadata_ for_ build_ wheel‘
Create a vertical seekbar from scratch
JMeter setting default startup Chinese
Ribbon's loadbalancerclient, zoneawareloadbalancer and zoneavoidancerule are three musketeers by default
Value on the object Z rotation synchronization panel in unity
Taurus. How to upgrade and run MVC on net6 and net7
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality
ThreeJs
Do you really understand the concept of buffer? Take you to uncover the buffer zone~
[leecode] get the longest common substring of two strings
How to solve the problem of robot positioning and navigation in large indoor scenes with low-cost solutions?
OpenGL drawing simple triangles
Spark Learning: Spark implementation of distcp
Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
Balance between management / business and technology