Basic Types -- IntBasic Types -- Intldots Basic Types -- BoolHaskell FunctionsHaskell Functionsldots Basic Types -- CharBasic Types -- TuplesBasic Types -- Tuplesldots The Offside RuleThe Offside Ruleldots Readings and ReferencesSummaryHomeworkHomeworkldots Homeworkldots Homeworkldots520—Spring 2005—11CSc 520Principles of ProgrammingLanguages11: Haskell — BasicsChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2005 Christian Collberg[1]520—Spring 2005—11Basic Types – IntAs we’ve seen, Haskell supports the integer (Int) type.Integer Operators:Op Precedence Associativity Descriptionˆ 8 right Exponentiation*, / 7 left Mul, Divdiv 7 free Divisionrem 7 free Remainder[2]520—Spring 2005—11Basic Types – Int...Op Precedence Associativity Descriptionmod 7 free Modulus+, - 6 left Add, Subtract==,/= 4 free (In-) Equality<,<=,>,>= 4 free Relational Compari-son1+2-3 ⇒ (1+2)-31+2*3 ⇒ 1+(2*3)2ˆ3ˆ4 ⇒ 2ˆ(3ˆ4)4==5==6 ⇒ ERROR12/6/3 ⇒ ERROR12/(6/3) ⇒ 6[3]520—Spring 2005—11Basic Types – BoolThere are two boolean literals, True and FalseOp Precedence Associativity Description&& 3 right logical and|| 2 right logical ornot 9 – logical not3 < 5 && 4 > 2 ⇔ (3 < 5) && (4 > 2)True || False && True ⇔ True || (False && True)[4]520—Spring 2005—11Haskell FunctionsHere’s the ubiquitous factorial function:fact :: Int -> Intfact n = if n == 0 then1elsen * fact (n-1)The first part of a function definition is the typesignature, which gives the domain and range of thefunction:fact :: Int -> IntThe second part of the definition is the functiondeclaration, the implementation of the function:fact n = if n == 0 then · · ·[5]520—Spring 2005—11Haskell Functions...The syntax of a type signature isfun name :: arg typesfacttakes one integer input argument and returns oneinteger result.The syntax of function declarations:fun name param names = fun bodyfact is defined recursively, i.e. the function bodycontains an application of the function itself.Function application examples:fact 1 ⇒ 1fact 5 ⇒ 120fact (3+2) ⇒ 120[6]520—Spring 2005—11Basic Types – CharLiterals: ’a’, ’b’. Special characters: ’\n’(newline).ASCII: ’\65’ (decimal), ’\x41’ (hex).toUpper, isAlpha, etc, are defined in the standardprelude.Built-in Functions:ord :: Char -> Intchar :: Int -> ChartoUpper, toLower :: Char -> CharisAscii,isDigit,· · · :: Char -> BoolisUpper,isLower,· · · :: Char -> Boolord ’a’ ⇒ 97 toUpper ’a’ ⇒ ’A’chr 65 ⇒ ’A’ isDigit ’a’ ⇒ False[7]520—Spring 2005—11Basic Types – TuplesA Haskell tuple is similar to a Pascal record – it is acollection of objects of (a limited number of) objects,possibly of different types. Each Pascal record elementshas a uniquename, whereas in Haskell you distinguishbetween elements by their position in the tuple.Syntax: (t1, t2, · · · , tn).Examples:type Complex = (Float,Float)mkComplex :: Float -> Float -> ComplexmkComplex re im = (re, im)[8]520—Spring 2005—11Basic Types – Tuples...type Complex = (Float,Float)mkComplex :: Float -> Float -> ComplexmkComplex re im = (re im)mkComplex 5 3 ⇒ (5, 3)addComplex :: Complex -> Complex -> ComplexaddComplex (a,b) (c,d) = (a+c,b+d)addComplex (mkComplex 5 3) (mkComplex 4 2)⇒ (9,5)[9]520—Spring 2005—11The Offside RuleWhen does one function definition end and the next onebegin?square x = x * x+2cube x =· · ·Textual layout determines when definitions begin andend.[10]520—Spring 2005—11The Offside Rule...The first character after the "=" opens up a box whichholds the right hand side of the equation:square x =x * x+2Any character to the left of the line closes the box andstarts a new definition:square x =x * x+2cube x = ...[11]520—Spring 2005—11Readings and ReferencesA free implementationftp://ftp.dcs.gla.ac.uk/pub/haskell/gofer.Compiler and interpreter available on linux:/home/cs520/2003/bin/linux/gofer.You can also use the Haskell compiler on lectura:/home/cs520/2003/ghc-5.04.1/bin/sparc-sun-solaris2/ghci.http://dmoz.org/Computers/Programming/Languages/Haskell.[12]520—Spring 2005—11SummaryHaskell has all the basic types one might expect: Ints,Chars, Floats, and Bools.Haskell functions come in two parts, the signature andthe declaration:fun name :: argument typesfunname param names = fun bodyMany Haskell functions will use recursion.Haskell doesn’t have assignment statements, loopstatements, or procedures.Haskell tuples are similar to records in other languages.[13]520—Spring 2005—11Homework1. Start (Mac || Unix) Haskell.2. Enter thecommaint function and try it out.3. Enter the addComplex and mkComplex functions andtry them out.4. Turn on tracing (Options:Trace Reductions inMacHaskell) and try the functions again.5. Try the standard functionsfst x and snd x oncomplex values. What do fst and snd do?6. Try out the Eliza application in Demos:Eliza.[14]520—Spring 2005—11Homework...Write a Haskell function to check if a character isalphanumeric, i.e. a lower case letter, upper case letter,or digit.? isAlphaNum ’a’True? isAlphaNum ’1’True? isAlphaNum ’A’True? isAlphaNum ’;’False? isAlphaNum ’@’False[15]520—Spring 2005—11Homework...Define a Haskell exclusive-or function.eOr :: Bool -> Bool -> BooleOr x y =· · ·? eOr True TrueFalse? eOr True FalseTrue? eOr False TrueTrue? eOr False FalseFalse[16]520—Spring 2005—11Homework...Define a Haskell function charToInt which converts adigit like ’8’ to its integer value 8. The value ofnon-digits should be taken to be 0.charToInt :: Char -> IntcharToInt c = · · ·? charToInt ’8’8? charToInt ’0’0? charToInt
View Full Document