欢迎来到天天文库
浏览记录
ID:40961305
大小:516.52 KB
页数:16页
时间:2019-08-12
《Sequential Erlang》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、ConditionalEvaluation:casecaselists:member(foo,List)oftrue->ok;false->{error,unknown}endErlangSolutionsLtd.SequentialErlang©1999-2011ErlangSolutionsLtd.©1999-2011ErlangSolutionsLtd.313Overview:sequentialErlangIConditionalEvaluation:case•SequentialErlangI-ConditionalEvaluation-Guards-Recursionc
2、aselists:member(foo,List)ofcasetrue->ok;•SequentialErlangIIclausesfalse->{error,unknown}•SequentialErlangIIIend©1999-2011ErlangSolutionsLtd.2©1999-2011ErlangSolutionsLtd.323Tuesday,6September2011Tuesday,6September2011ConditionalEvaluation:caseConditionalEvaluation:caseexpressionclauseseparator
3、caselists:member(foo,List)ofcaselists:member(foo,List)oftrue->ok;true->ok;false->{error,unknown}false->{error,unknown}endend©1999-2011ErlangSolutionsLtd.3©1999-2011ErlangSolutionsLtd.333ConditionalEvaluation:caseConditionalEvaluation:casecaselists:member(foo,List)ofcaselists:member(foo,List)of
4、true->ok;true->ok;false->{error,unknown}false->{error,unknown}endendnoseparatorinlastclausepatterns©1999-2011ErlangSolutionsLtd.3©1999-2011ErlangSolutionsLtd.333Tuesday,6September2011Tuesday,6September2011Conditionalevaluation:caseConditionalEvaluation:ifcaseof•Onebranchshouldalway
5、sPattern1->succeed,,•Usinganunboundvariableor'_'ensuresthat...theclausewillalwaysif;Pattern2->matchX<1->smaller;,X>1->greater;•The_clauseisnot,mandatoryX==1->equal...end;•Anexceptionisraisedif_->noclausematches6、ssion1>,...•Returnsthevalueofthelastexecutedexpressionend©1999-2011ErlangSolutionsLtd.4©1999-2011ErlangSolutionsLtd.646DefensiveProgrammingConditionalEvaluation:ifconvert(Day)->•Defensiveprogramming:caseDayofprogramintheconvertmonday->1;functionfortheerrorcasetuesday->2;or...wedne7、sday->3;•...letitfailherebyifthursday->4;friday->5;deletingtheOtherclause.X<1->smaller;saturday->6;ifclausesX>1->greater;•Thiswillraiseansunday->7;exceptionX==1->equalOther->end{error,unknown_day}•Thecallerwillhavetoend.handletheerrorth
6、ssion1>,...•Returnsthevalueofthelastexecutedexpressionend©1999-2011ErlangSolutionsLtd.4©1999-2011ErlangSolutionsLtd.646DefensiveProgrammingConditionalEvaluation:ifconvert(Day)->•Defensiveprogramming:caseDayofprogramintheconvertmonday->1;functionfortheerrorcasetuesday->2;or...wedne
7、sday->3;•...letitfailherebyifthursday->4;friday->5;deletingtheOtherclause.X<1->smaller;saturday->6;ifclausesX>1->greater;•Thiswillraiseansunday->7;exceptionX==1->equalOther->end{error,unknown_day}•Thecallerwillhavetoend.handletheerrorth
此文档下载收益归作者所有