您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Beginning Haskell
BeginningHaskellPresentedbydeveloperWorks,yoursourceforgreattutorialsibm.com/developerWorksTableofContentsIfyou'reviewingthisdocumentonline,youcanclickanyofthetopicsbelowtolinkdirectlytothatsection.1.Aboutthistutorial22.Haskellbasics33.Takingthevows44.Anewexpressiveness75.Modulesandprogramstructure166.Resourcesandfeedback21BeginningHaskellPage1Section1.AboutthistutorialShouldItakethistutorial?ThistutorialtargetsprogrammersofimperativelanguageswantingtolearnaboutfunctionalprogramminginthelanguageHaskell.IfyouhaveprogrammedinlanguagessuchasC,Pascal,Fortran,C++,Java,Cobol,Ada,Perl,TCL,REXX,JavaScript,VisualBasic,ormanyothers,youhavebeenusinganimperativeparadigm.Thistutorialprovidesagentleintroductiontotheparadigmoffunctionalprogramming,withspecificillustrationsintheHaskell98language.Programmerswithabackgroundinfunctionalprogrammingwillprobablyfindthistutorialabitslow;however,programmerswhohavenotusedHaskell98inparticularcanstillgetaquicksenseofthelanguagebybrowsingthetutorial.What'snotcovered?Inanintroductorytutorial,manyofHaskell'smostpowerfulandcomplexfeaturescannotbecovered.Inparticular,thewholeareaoftypeclassesandalgebraictypes(includingabstractdatatypes)isabitmuchforafirstintroduction.Forreaderswhoseinterestispiqued,IwillmentionthatHaskellallowsyoutocreateyourowndatatypes,andtoinheritpropertiesofthosedatatypesintypeinstances.TheHaskelltypesystemcontainsthefundamentalfeaturesofobject-orientedprogramming(inheritance,polymorphism,encapsulation);butinawaythatisalmostimpossibletograspwithinaC++/Java/Smalltalk/Eiffelstyleofthinking.Theothersignificantelementomittedinthistutorialisadiscussionofmonads,andthereforeofI/O.ItseemsstrangetowriteatutorialthatdoesnotevenstartwithaHelloWorld!program,butthinkinginafunctionalstylerequiresanumberofshifts.WhilethatHelloWorld!isquitesimple,italsoinvolvestheminipseudo-imperativeworldofmonads.Itwouldbeeasyforabeginnertobelulledbythepseudo-imperativestyleofI/O,andmisswhatisreallygoingon.Swimmingisbestlearnedbygettinginthewater.AbouttheauthorDavidMertzisawriter,aprogrammer,andateacher,whoalwaysendeavorstoimprovehiscommunicationtoreaders(andtutorialtakers).Hewelcomesanycommentsorquestionsaboutthistutorial;pleasedirectthemtomertz@gnosis.cx.PresentedbydeveloperWorks,yoursourceforgreattutorialsibm.com/developerWorksBeginningHaskellPage2Section2.HaskellbasicsAboutHaskellHaskellisjustoneofanumberoffunctionalprogramminglanguages.OthersincludeLisp,Scheme,Erlang,Clean,Mercury,ML,OCaml,andothers.ThecommonadjunctlanguagesSQLandXSLarealsofunctional.Likefunctionallanguages,logicalorconstraint-basedlanguageslikePrologaredeclarative.Incontrast,bothproceduralandobject-orientedlanguagesare(broadlyspeaking)imperative.Somelanguages,suchasPython,Scheme,Perl,andRuby,crosstheseparadigmboundaries;but,forthemostpart,programminglanguageshaveaparticularprimaryfocus.Amongfunctionallanguages,Haskellisinmanywaysthemostidealizedlanguage.Haskellisapurefunctionallanguage,whichmeansiteschewsallsideeffects(morelater).Haskellhasanon-strictorlazyevaluationmodel,andisstrictlytyped(butwithtypesthatallowadhocpolymorphism).Otherfunctionallanguagesdifferineachofthesefeatures--forreasonsimportanttotheirdesignphilosophies--butthiscollectionoffeaturesbringsone,arguably,farthestintothefunctionalwayofthinkingaboutprograms.Onaminornote,HaskellissyntacticallyeasiertogetahandleonthanaretheList-derivedlanguages(especiallyforprogrammerswhohaveusedlightlypunctuatedlanguageslikePython,TCL,andREXX).Mostoperatorsareinfixedratherthanprefixed.Indentationandmoduleorganizationlooksprettyfamiliar.Andperhapsmoststrikingly,theextremedepthofnestedparentheses(asseeninLisp)isavoided.ObtainingHaskellHaskellhasseveralimplementationsformultipleplatforms.TheseincludebothaninterpretedversioncalledHugs,andseveralHaskellcompilers.ThebeststartingplaceforalloftheseisHaskell.org.LinksleadtovariousHaskellimplementations.Dependingonyouroperatingsystem,anditspackagingsystem,Haskellmayhavealreadybeeninstalled,ortheremaybeastandardwaytoinstallaready-to-runversion.IrecommendthosetakingthistutorialobtainHugsforpurposesofinitialexperimentation,andforworkingalongwiththistutorial,ifyouwishtodoso.PresentedbydeveloperWorks,yoursourceforgreattutorialsibm.com/developerWorksBeginningHaskellPage3Section3.TakingthevowsGivingthingsupThemostdifficultpartofstartingtoprogramwithHaskellisgivingupmanyofthemostfamiliartechniquesandwaysofthinkingwithinimperativeprogramming.AfirstimpressionisoftenthatitmustsimplybeimpossibletowriteacomputerprogramifyoucannotdoX,Y,orZ,especiallysinceX,Y,andZaresomeofthemostcommonpatternsinnormalimperativeprogramming.Inthissection,let'sreviewafewofthemostshockingfeaturesofHaskell(andoffunctionalprogrammingingeneral).NomutablevariablesOneofthemostcommonprogramminghabitsinimperativeprogrammingistoassignavariableonevalue,thenassignitadifferentvalue;perhapsalongthewaywetestwhetherthevariablehasobtainedcertainkeyvalues.ConstructsliketheCexamplesbelowareubiquitous(otherimperativelanguagesaresimilar):if(myVar==37){...}myVar+=2for(myVar=0;myVar37;myVar++
本文标题:Beginning Haskell
链接地址:https://www.777doc.com/doc-5536532 .html