Lecture - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture

Description:

The prelude functions. Strings and Characters. Lazy Evaluation. Infinite Lists ... interesting functions in the prelude. Cse536 Functional Programming. 6. Lazy ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 23
Provided by: timsh3
Learn more at: http://web.cecs.pdx.edu
Category:
Tags: lecture | prelude

less

Transcript and Presenter's Notes

Title: Lecture


1
Lecture 3, Oct 4, 2004
  • Reading Assignments
  • Finish chapter 2 and begin Reading chapter 3 of
    the Text
  • Todays Topics
  • Useful functions on lists
  • The prelude functions
  • Strings and Characters
  • Lazy Evaluation
  • Infinite Lists
  • Data-Type declarations
  • Defining functions over datatypes using patterns
  • Enumerations
  • The Shape Datatype of the text

2
Useful Functions on lists
  • Inspect the Haskell Prelude for a complete list
  • Let
  • x 1,2,3,4
  • y "a","b","c","d"
  • Pair-wise destructors
  • head x 1 tail x 2,3,4
  • init x 1,2,3 last x 4
  • take 2 x 1,2 drop 2 x 3,4
  • takeWhile odd x 1 dropWhile odd x 2,3,4
  • Useful Auxiliary
  • reverse x 4,3,2,1
  • zip x y (1,"a"), (2,"b"), (3,"c"), (4,"d")

3
Useful Functions on lists
  • Higher Order Functions
  • Let
  • x 1,2,3,4
  • y "a","b","c","d"
  • Then
  • map f x f 1, f 2, f 3, f 4
  • filter even x 2,4
  • foldr () e x
  • 1 (2 (3 (4 e)))
  • foldl () e x
  • (((e 1) 2) 3) 4
  • iterate f 1
  • 1, f 1, f(f 1),f(f(f 1)), ...

4
String and Char
  • String is defined to be a (List Char)
  • ? "abc"
  • abc
  • ? 'a', 'b', 'c'
  • abc
  • Be Careful !! Know the difference between
  • "a" is a string (A List of Char with one element)
  • 'a' is a Char
  • a is an operator (note the back-quotes)
  • Since String List Char all the list operations
    work on strings
  • ? abc xyz
  • abcxyz
  • ? reverse abc
  • cba

5
Quotation Mechanisms
  • The normal (C-like) back-slash notation is used
    to embed special characters in String's and
    Char's
  • '' colon
  • '\'' single quote
  • '\"' double quote
  • '\\' back-slash
  • '\n' newline
  • '\t' tab
  • They can also be used in String's
  • "A two \n line string"
  • Conversion Functions
  • ord Char -gt Int
  • chr Int -gt Char
  • Lots of interesting functions in the prelude.

6
Lazy Evaluation(GITH pp 12-14)
  • Consider
  • repeat x x (repeat x)
  • ? repeat 't'
  • ttttttttttttttttttttttttttttttttttttttttttttttttt
    tttttCInterrupted!
  • Ok to use a finite Prefix
  • ? take 10 (repeat 't')
  • tttttttttt
  • Remember iterate?
  • ? take 5 (iterate (1) 1)
  • 1, 2, 3, 4, 5
  • ? take 5 (iterate (2) 1)
  • 1, 2, 4, 8, 16
  • ? take 5 (iterate (/10) 142)
  • 142, 14, 1, 0, 0

7
Computing Square Root
  • Approximating the square root.
  • Compute the n1 approximation from the nth
    approximation of square root of X
  • an1 ( an N / an ) / 2
  • The Haskell function
  • next n a (a (n/a) )/ 2.0
  • Example Use
  • ? take 7 (iterate (next 2.0) 1.0)
  • 1.0, 1.5, 1.41667, 1.41422, 1.41421, 1.41421,
    1.41421
  • How do we know when to stop?
  • 1.0, 1.5, 1.41667, 1.41422,
    ...
  • .5 .09 .002

8
Small Changes
  • Chop off a list when two successive elements are
    within some small epsilon of each other.
  • within eps (a b rest)
  • if abs( (a/b) -1.0 ) lt eps
  • then b
  • else within eps (b rest)
  • Now square root is easy
  • ? within 0.0001 (iterate (next 2.0) 1.0)
  • 1.41421

9
Defining New Datatypes
  • Ability to add new datatypes in a programming
    language is important.
  • Kinds of datatypes
  • enumerated types
  • records (or products or struct)
  • variant records (or sums)
  • pointer types
  • arrays
  • Haskells data declaration provides many of
    these kinds of types in a uniform way which
    abstracts from their implementation details.
  • The data declaration defines new functions and
    constants, which provide an abstract interface to
    the newly defined type.

10
The Data Declaration
  • Enumeration Types
  • data Day Sun Mon Tue Wed Thu Fri
    Sat
  • deriving Eq
  • The names on right hand side are constructor
    constants and are the only elements of the type.
  • valday 0 Sun
  • valday 1 Mon
  • valday 2 Tue
  • valday 3 Wed
  • valday 4 Thu
  • valday 5 Fri
  • valday 6 Sat
  • ? valday 3
  • Wed

11
Constructors Patterns
  • data defined types define new constructors and
    can be accessed by patterns.
  • Constructors without arguments are constants
  • Example using case
  • dayval x
  • case x of
  • Sun -gt 0
  • Mon -gt 1
  • Tue -gt 2
  • Wed -gt 3 Thu -gt 4 Fri -gt 5
  • Sat -gt 6
  • Note Indentation bounds each clause ( pat -gt
    body) in case, or use a semi-colon rather than
    indentation.

12
Patterns in Declarations
  • In a declaration patterns can be used. Possible
    to have many lines for a definition if each
    pattern is distinct.
  • dayval Sun 0
  • dayval Mon 1
  • dayval Tue 2
  • dayval Wed 3
  • dayval Thu 4
  • dayval Fri 5
  • dayval Sat 6
  • ? dayval Tue
  • 2

13
Patterns are not always necessary
  • An alternate definition might be
  • dayval Day -gt Int
  • dayval x
  • let (d,n) head (filter (\(d,n)-gtdx)
  • (zip Sun,Mon,Tue,Wed,Thu,Fri,Sat
  • 0..))
  • in n
  • Note use of 0..
  • 0.. denotes 0,1,2,3, ...
  • Why is an infinite list possible here?
  • Give a definition of zip using patterns over
    lists ( and (xxs)
  • zip

14
Example function
  • dayafter d
  • valday (((dayval d) 1) mod 7)
  • ? dayafter Tue
  • Wed

15
Other Enumeration Examples
  • data Move Paper Rock Scissors
  • beats Move -gt Move
  • beats Paper Scissors
  • beats Rock Paper
  • beats Scissors Rock
  • ? beats Paper
  • Scissors
  • data Bool True False
  • data Direction North East South West

16
Variant Records
  • More complicated types
  • data Tagger Tagn Int Tagb Bool
  • NOTE the types of the constructors are functions
    (not constants as for enumerated types)
  • Tagb Bool -gt Tagger
  • Tagn Int -gt Tagger
  • As for all constructors
  • (Tagn 12)
  • 1) Cannot be simplified. We say it is Canonical
  • 2) Can be used in a pattern on the left hand
    side of an

17
Example functions on Tagger
  • number (Tagn n) n
  • boolean (Tagb b) b
  • isNum (Tagn _) True
  • isNum (Tagb _) False
  • ? t number
  • number Tagger -gt Int
  • ? number (Tagn 3)
  • 3
  • ? isNum (Tagb False)
  • False

18
Another Variant Record-like Type
  • data Temp Celsius Float
  • Fahrenheit Float
  • Kelvin Float
  • Use patterns to define functions over this type
  • toKelvin (Celsius c) Kelvin(c 273.0)
  • toKelvin (Fahrenheit f)
  • Kelvin( (f - 32.0) (5.0/9.0) 273.0 )
  • toKelvin (Kelvin k) Kelvin k

19
Shape types from the Text
  • data Shape Rectangle Float Float
  • Ellipse Float Float
  • RtTriangle Float Float
  • Polygon (Float,Float)
  • deriving Show
  • Deriving Show
  • tells the system to build an show function for
    the type Shape
  • Using Shape - Functions returning shape objects
  • circle radius Ellipse radius radius
  • square side Rectangle side side

20
Functions over Shape
  • Functions over shape can be defined using pattern
    matching
  • area Shape -gt Float
  • area (Rectangle s1 s2) s1 s2
  • area (Ellipse r1 r2) pi r1 r2
  • area (RtTriangle s1 s2) (s1 s2) / 2
  • area (Polygon (v1pts)) polyArea pts
  • where polyArea (Float,Float) -gt Float
  • polyArea (v2 v3 vs) triArea v1 v2
    v3
  • polyArea
    (v3vs)
  • polyArea _ 0
  • Note use of prototype
  • Note use of nested patterns
  • Note use of wild card pattern (matches anything)

21
Poly A,B,C,D,E,F
A
Area Area(Triangle A,B,C)
Area(PolyA,C,D,E,F)
B
F
C
E
D
22
TriArea
  • triArea v1 v2 v3
  • let a distBetween v1 v2
  • b distBetween v2 v3
  • c distBetween v3 v1
  • s 0.5(abc)
  • in sqrt (s(s-a)(s-b)(s-c))
  • distBetween (x1,y1) (x2,y2)
  • sqrt ((x1-x2)2 (y1-y2)2)
Write a Comment
User Comments (0)
About PowerShow.com