2
2VALUES,TYPES,ANDOTHERGOODIES
otherhand,Haskell'sStandardPrelude(inAppendixAoftheReportandthestandardlibraries(foundintheLibraryReport[5])containlotsofusefulexamplesofHaskellcode;weencourageathoroughreadingoncethistutorialiscompleted.ThiswillnotonlygivethereaderafeelforwhatrealHaskellcodelookslike,butwillalsofamiliarizeherwithHaskell'sstandardsetofpredefinedfunctionsandtypes.Finally,theHaskellwebsite,
http://haskell.~rg,
hasawealthofinformationabouttheHaskelllanguageanditsimplementations.[Wehavealsotakenthecourseofnotlayingoutaplethoraoflexicalsyntaxrulesattheoutset.Rather,weintroducethemincrementallyasourexamplesdemand,andenclosetheminbrackets,aswiththisparagraph.ThisisinstarkcontrasttotheorganizationoftheReport,althoughtheReportremainstheauthoritativesourcefordetails(referencessuchas"§2.1"refertosectionsintheReport).JHaskellisa
typeful
programminglanguage.!typesarepervasive,andthenewcomerisbestoffbecomingwellawareofthefullpowerandcomplexityofHaskell'stypesystemfromtheoutset.Forthosewhoseonlyexperienceiswithrelatively"untypeful"languagessuchasPerl,Tel,orScheme,thismaybeadifficultadjustment;forthosefamiliarwithJava,C,Modula,orevenML,theadjustmentshouldbeeasierbutstillnotinsignificant,sinceHaskell'stypesystemisdifferentandsomewhatricherthanmost.Inanycase,"typefulprogramming"ispartoftheHaskellprogrammingexperience,andcannotbeavoided.
2Values,Types,andOtherGoodies
BecauseHaskellisapurelyfunctionallanguage,allcomputationsaredoneviatheevaluationof
expressions
(syntacticterms)toyield
values
(abstractentitiesthatweregardasanswers).Everyvaluehasanassociated
type.
(Intuitively,wecanthinkoftypesassetsofvalues.)Examplesofexpressionsincludeatomicvaluessuchastheinteger5,thecharacter'a',andthefunction
\x
->
x+1,
aswellasstructuredvaluessuchasthelist
[1,2,3]
andthepair
('b',4).
Justasexpressionsdenotevalues,typeexpressionsaresyntactictermsthatdenotetypevalues(orjust
types).
Examplesoftypeexpressionsincludetheatomictypes
Integer
(infinite-precisionintegers),
Char
(characters),
Integer->Integer
(functionsmapping
Integer
to
Integer),
aswellasthestructuredtypes
[Integer]
(homogeneouslistsofintegers)and
(Char,Integer)
(character,integerpairs).AllHaskellvaluesare"first-class"-theymaybepassedasargumentstofunctions,returnedasresults,placedindatastructures,etc.Haskelltypes,ontheotherhand,are
not
first-class.Typesinasensedescribevalues,andtheassociationofavaluewithitstypeiscalleda
typing.
Usingtheexamplesofvaluesandtypesabove,wewritetypingsasfollows:
5
IntegerCharInteger->Integer[Integer](Char,Integer)
'a'
inc
[1,2,3]
('b',4)
lCoined
by
LucaCardelli.