您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > haskell-tutorial
AGentleIntroductiontoHaskell98PaulHudakYaleUniversityDepartmentofComputerScienceJohnPetersonYaleUniversityDepartmentofComputerScienceJosephH.FaselUniversityofCaliforniaLosAlamosNationalLaboratoryOctober,1999Copyrightc 1999PaulHudak,JohnPetersonandJosephFaselPermissionisherebygranted,freeofcharge,toanypersonobtainingacopyof\AGentleIntroductiontoHaskell(theText),todealintheTextwithoutrestriction,includingwithoutlimitationtherightstouse,copy,modify,merge,publish,distribute,sublicense,and/orsellcopiesoftheText,andtopermitpersonstowhomtheTextisfurnishedtodoso,subjecttothefollowingcondition:TheabovecopyrightnoticeandthispermissionnoticeshallbeincludedinallcopiesorsubstantialportionsoftheText.1IntroductionOurpurposeinwritingthistutorialisnottoteachprogramming,noreventoteachfunctionalprogramming.Rather,itisintendedtoserveasasupplementtotheHaskellReport[4],whichisotherwisearatherdensetechnicalexposition.OurgoalistoprovideagentleintroductiontoHaskellforsomeonewhohasexperiencewithatleastoneotherlanguage,preferablyafunctionallanguage(evenifonlyan\almost-functionallanguagesuchasMLorScheme).Ifthereaderwishestolearnmoreaboutthefunctionalprogrammingstyle,wehighlyrecommendBird’stextIntroductiontoFunctionalProgramming[1]orDavie’sAnIntroductiontoFunctionalProgrammingSystemsUsingHaskell[2].Forausefulsurveyoffunctionalprogramminglanguagesandtechniques,includingsomeofthelanguagedesignprinciplesusedinHaskell,see[3].TheHaskelllanguagehasevolvedsigni cantlysinceitsbirthin1987.ThistutorialdealswithHaskell98.Olderversionsofthelanguagearenowobsolete;HaskellusersareencouragedtouseHaskell98.TherearealsomanyextensionstoHaskell98thathavebeenwidelyimplemented.ThesearenotyetaformalpartoftheHaskelllanguageandarenotcoveredinthistutorial.Ourgeneralstrategyforintroducinglanguagefeaturesisthis:motivatetheidea,de nesometerms,givesomeexamples,andthenpointtotheReportfordetails.Wesuggest,however,thatthereadercompletelyignorethedetailsuntiltheGentleIntroductionhasbeencompletelyread.Onthe122VALUES,TYPES,ANDOTHERGOODIESotherhand,Haskell’sStandardPrelude(inAppendixAoftheReportandthestandardlibraries(foundintheLibraryReport[5])containlotsofusefulexamplesofHaskellcode;weencourageathoroughreadingoncethistutorialiscompleted.ThiswillnotonlygivethereaderafeelforwhatrealHaskellcodelookslike,butwillalsofamiliarizeherwithHaskell’sstandardsetofprede nedfunctionsandtypes.Finally,theHaskellwebsite,[Wehavealsotakenthecourseofnotlayingoutaplethoraoflexicalsyntaxrulesattheoutset.Rather,weintroducethemincrementallyasourexamplesdemand,andenclosetheminbrackets,aswiththisparagraph.ThisisinstarkcontrasttotheorganizationoftheReport,althoughtheReportremainstheauthoritativesourcefordetails(referencessuchas\x2.1refertosectionsintheReport).]Haskellisatypefulprogramminglanguage:1typesarepervasive,andthenewcomerisbesto becomingwellawareofthefullpowerandcomplexityofHaskell’stypesystemfromtheoutset.Forthosewhoseonlyexperienceiswithrelatively\untypefullanguagessuchasPerl,Tcl,orScheme,thismaybeadiÆcultadjustment;forthosefamiliarwithJava,C,Modula,orevenML,theadjustmentshouldbeeasierbutstillnotinsigni cant,sinceHaskell’stypesystemisdi erentandsomewhatricherthanmost.Inanycase,\typefulprogrammingispartoftheHaskellprogrammingexperience,andcannotbeavoided.2Values,Types,andOtherGoodiesBecauseHaskellisapurelyfunctionallanguage,allcomputationsaredoneviatheevaluationofexpressions(syntacticterms)toyieldvalues(abstractentitiesthatweregardasanswers).Everyvaluehasanassociatedtype.(Intuitively,wecanthinkoftypesassetsofvalues.)Examplesofexpressionsincludeatomicvaluessuchastheinteger5,thecharacter’a’,andthefunction\x-x+1,aswellasstructuredvaluessuchasthelist[1,2,3]andthepair(’b’,4).Justasexpressionsdenotevalues,typeexpressionsaresyntactictermsthatdenotetypevalues(orjusttypes).ExamplesoftypeexpressionsincludetheatomictypesInteger(in nite-precisionintegers),Char(characters),Integer-Integer(functionsmappingIntegertoInteger),aswellasthestructuredtypes[Integer](homogeneouslistsofintegers)and(Char,Integer)(character,integerpairs).AllHaskellvaluesare\ rst-class|theymaybepassedasargumentstofunctions,returnedasresults,placedindatastructures,etc.Haskelltypes,ontheotherhand,arenot rst-class.Typesinasensedescribevalues,andtheassociationofavaluewithitstypeiscalledatyping.Usingtheexamplesofvaluesandtypesabove,wewritetypingsasfollows:5::Integer’a’::Charinc::Integer-Integer[1,2,3]::[Integer](’b’,4)::(Char,Integer)1CoinedbyLucaCardelli.2.1PolymorphicTypes3The\::canberead\hastype.FunctionsinHaskellarenormallyde nedbyaseriesofequations.Forexample,thefunctioninccanbede nedbythesingleequation:incn=n+1Anequationisanexampleofadeclaration.Anotherkindofdeclarationisatypesignaturedecla-ration(x4.4.1),withwhichwecandeclareanexplicittypingforinc:inc::Integer-IntegerWewillhavemuchmoretosayaboutfunctionde nitionsinSection3.Forpedagogicalpurposes,whenwewishtoindicatethatanexpressione1evaluates,or\re-duces,toanotherexpressionorvaluee2,wewillw
本文标题:haskell-tutorial
链接地址:https://www.777doc.com/doc-4350473 .html