DOC PREVIEW
UA CSC 520 - Lecture Notes

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSc 520Principles of Programming Languages11: Haskell — BasicsChristian CollbergDepartment of Computer ScienceUniversity of [email protected] 2005 Christian CollbergApril 22, 20051 Basic Types – Int• As 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 Remainder2 Basic Types – Int. . .Op Precedence Associativity Descriptionmod 7 free Modulus+, - 6 left Add, Subtract==,/= 4 free (In-) Equality<,<=,>,>= 4 free Relational Comparison1+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) ⇒ 63 Basic Types – Bool• There are two boolean literals, True and False1Op 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 Haskell Functions• Here’s the ubiquitous factorial function:fact :: Int -> Intfact n = if n == 0 then1elsen * fact (n-1)• The first part of a function definition is thetype signature, which gives the domain and range of thefunction:fact :: Int -> Int• The second part of the definition is thefunction declaration, the implementation of the function:fact n = if n == 0 then · · ·5 Haskell Functions. . .• The syntax of a type signature isfun name :: arg typesfact takes one integer input argument and returns one integer result.• The syntax of function declarations:fun name param names = fun body• fact is defined recursively, i.e. the function body contains an application of the function itself.• Function application examples:fact 1 ⇒ 1fact 5 ⇒ 120fact (3+2) ⇒ 12026 Basic Types – Char• Literals: ’a’, ’b’. Special characters: ’\n’ (newline).• ASCII: ’\65’ (decimal), ’\x41’ (hex).• toUpper, isAlpha, etc, are defined in the standard prelude.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’ ⇒ False7 Basic Types – Tuples• A Haskell tuple is similar to a Pascal record – it is a collection of objects of (a limited number of)objects, possibly of different types. Each Pascal record elements has a uniquename, whereas in Haskellyou distinguish between 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 Basic 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 The Offside Rule• When does one function definition end and the next one begin?square x = x * x+2cube x = · · ·• Textual layout determines when definitions begin and end.310 The Offside Rule. . .• The first character after the "=" opens up a box which holds the right hand side of the equation:square x =x * x+2• Any character to the left of the line closes the box and starts a new definition:square x =x * x+2cube x = ...11 Readings and References• A free implementation ftp://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 Summary• Haskell has all the basic types one might expect: Ints, Chars, Floats, and Bools.• Haskell functions come in two parts, the signature and the declaration:fun name :: argument typesfun name param names = fun body• Many Haskell functions will use recursion.• Haskell doesn’t have assignment statements, loop statements, or procedures.• Haskell tuples are similar to records in other languages.13 Homework1. Start (Mac || Unix) Haskell.2. Enter the commaint function and try it out.3. Enter the addComplex and mkComplex functions and try them out.4. Turn on tracing (Options:Trace Reductions in MacHaskell) and try the functions again.5. Try the standard functions fst x and snd x on complex values. What do fst and snd do?6. Try out the Eliza application in Demos:Eliza.414 Homework. . .• Write a Haskell function to check if a character is alphanumeric, i.e. a lower case letter, upper caseletter, or digit.? isAlphaNum ’a’True? isAlphaNum ’1’True? isAlphaNum ’A’True? isAlphaNum ’;’False? isAlphaNum ’@’False15 Homework. . .• Define a Haskell exclusive-or function.eOr :: Bool -> Bool -> BooleOr x y = · · ·? eOr True TrueFalse? eOr True FalseTrue? eOr False TrueTrue? eOr False FalseFalse16 Homework. . .• Define a Haskell function charToInt which converts a digit like ’8’ to its integer value 8. The valueof non-digits should be taken to be 0.charToInt :: Char -> IntcharToInt c = · · ·? charToInt ’8’8? charToInt ’0’0? charToInt


View Full Document

UA CSC 520 - Lecture Notes

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?