资源描述:
《Advanced Constructs》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、ListComprehensions[Expression
2、
3、Generator1,...,GeneratorN,Filter1,...,FilterN]•Afeaturecommoninfunctionalprogramminglanguages•AnalogoustosetcomprehensionsintheZermelo-FrankelsettheoryErlangSolutionsLtd.•AsyntacticalandsemanticalnotationtogeneratelistsAdvancedConstructs©199
4、9-2011ErlangSolutionsLtd.©1999-2011ErlangSolutionsLtd.313AdvancedConstructsListComprehensions•ListComprehensions•Binaries[X
5、
6、X<-[1,2,3,4],X<3]•References•Operators•TheaboveexampleshouldbereadasthelistofXwhereX•Macroscomesfromthelist[1,2,3,4]andXislessthan3•IncludeFiles•Pa
7、ttern<-Lististhegenerator•Filteriseitherabooleanexpressionorafunctionwhichreturnstrueorfalse©1999-2011ErlangSolutionsLtd.2©1999-2011ErlangSolutionsLtd.424Tuesday,6September2011Tuesday,6September2011ListComprehensions:examplesListComprehensions:examples1>[Int
8、
9、Int<-[zero,1
10、,two,three,4,5],perm([])->1>is_integer(Int)].[[]];[1,4,5]perm(List)->2>[{X,Y}
11、
12、X<-[1,2,3],Y<-[a,b,c]].[[H
13、T]
14、
15、H<-List,T<-perm(List--[H])].[{1,a},{1,b},{1,c},{2,a},{2,b},{2,c},{3,a},{3,b},{3,c}]3>[X
16、
17、X<-[1,2,3,4],Y<-[3,4,5,6],X==Y].[3,4]4>[X+1
18、
19、X<-[1,2,3,4],Xrem2==0].[3,5]
20、•Filtering,cartesianproducts,intersections,andselective•perm([c,a,t])->[[c,a,t],[c,t,a],[a,c,t],[a,t,c],[t,c,a],[t,a,c]]mappingusinglistcomprehensions•WetakeHfromListinallpossibleways,andappendallpermutationsofListwithHremovedtoit©1999-2011ErlangSolutionsLtd.5©1999-2011Er
21、langSolutionsLtd.757ListComprehensions:examplesListComprehensions:variablesmap(Fun,List)->•Allvariablesinthegeneratorpatternare[Fun(X)
22、
23、X<-List].filter(Predicate,List)->consideredfresh[X
24、
25、X<-List,Predicate(X)].append(ListOfLists)->•Boundvariablesinthegeneratorandbeforethe
26、[X
27、
28、List<-ListOfLists,X<-List].LCexpressionwhichareusedinthefilterretaintheirvalue•NovariablecanbeexportedfromaLCexpression•Rewritinglistslibraryfunctionsusinglistcomprehensions•Thecompilergivesawarningwhenyoushadowvariables©1999-2011ErlangSolutionsLtd.6©1999-2011ErlangSo
29、lutionsLtd.868Tuesday,6September2011Tuesday,6September2011ListComprehensions:variablesBinaries1>