Todays Topics - PowerPoint PPT Presentation

About This Presentation
Title:

Todays Topics

Description:

'six','seven','eight', 'nine'] teens = ['ten','eleven','twelve','thirteen','fourteen' ... 'sixty','seventy','eighty','ninety'] Split into tens and ones ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 17
Provided by: timsh3
Learn more at: http://web.cecs.pdx.edu
Category:
Tags: nine | sixty | todays | topics

less

Transcript and Presenter's Notes

Title: Todays Topics


1
Lecture 2.5
  • Todays Topics
  • A Few Less Trivial Examples
  • Numerical Functions
  • Differentiation and square root. pp 30-32
    Fokker Notes
  • Primes. pp 29 Fokker Notes
  • Display Tool. pp 105 Reade Book
  • Numbers in Long Hand
  • Sorting. pp 106-109 Reade Book
  • Making Change. Bird Wadler
  • Reading Assignment
  • Begin reading chapter 2 in
  • The Haskell School of Expression

2
Numerical Functions
  • Differentiating a function
  • diff (Float -gt Float) -gt (Float -gt Float)
  • diff f f'
  • where f' x (f (xh) - f x)/ h
  • h 0.0001
  • For Small enough h, good approximation of slope
    of f

3
Square Root Again!
  • Repetition on functions
  • until (a -gt Bool) -gt (a -gt a) -gt a -gt a
  • ? until (gt10) (1) 1
  • 11
  • ? until (lt 0.001) (/2.0) 1.0
  • 0.000976562
  • Next approximation (again)
  • next n a (a (n/a) )/ 2.0
  • Almost Equal
  • infix 5
  • a b abs(a-b)lth abs(b-a)lth
  • where h 0.00001
  • Square Root
  • sqrRoot n until goodenough (next n) 1.0
  • where goodenough p pp n

4
Lists of Prime Numbers
  • Does one number evenly divide another?
  • divisible Int -gt Int -gt Bool
  • divisible t n t rem n 0
  • denominators Int -gt Int
  • denominators x filter (divisible x) 1..x
  • ? denominators 235
  • 1, 5, 47, 235
  • A Prime has only two denominators
  • prime Int -gt Bool
  • prime x denominators x 1,x
  • ? prime 12
  • False
  • ? prime 37
  • True

5
Primes (cont.)
  • To get a list of primes then generate and prune.
  • primes Int -gt Int
  • primes x filter prime 1..x
  • ? primes 30
  • 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
  • What efficiency concerns does this algorithm have?

6
A List Display Tool
  • How do we print? 1,2,3,4
  • pl Text a gt a -gt Char
  • pl ""
  • pl (xxs) (show x) "," (pl xs)
  • ? pl 1,2
  • 1,2,
  • How do we get rid of the annoying last ","
  • pl Text a gt a -gt Char
  • pl ""
  • pl x (show x)
  • pl (xxs) (show x) "," (pl xs)
  • ? pl 1,2,3
  • 1,2,3

7
List Display Tool (cont.)
  • What about the and
  • pl Text a gt a -gt Char
  • pl l "" (p l) ""
  • where p ""
  • p x (show x)
  • p (xxs) (show x) "," (p xs)
  • ? pl 1,2,3,4
  • 1,2,3,4
  • ? pl True, not True
  • True,False

8
Generalize
  • let and be parameters
  • pl Text a gt Char -gt Char -gt Char -gt a
    -gt Char
  • pl front back sep l
  • front (p l) back
  • where p ""
  • p x (show x)
  • p (xxs) (show x)
  • sep (p xs)
  • ? pl "" "" " " 1,2,3
  • 1 2 3
  • ? pl "(" ")" "," 1,2,3
  • (1,2,3)

9
Writing out numbers in long hand
  • 342 -gt three hundred forty-two
  • Solve for two digit numbers first
  • units "one","two","three","four","five",
  • "six","seven","eight", "nine"
  • teens "ten","eleven","twelve","thirteen","fourt
    een",
  • "fifteen","sixteen", "seventeen","eighteen",
  • "nineteen"
  • tens "twenty","thirty","forty","fifty",
  • "sixty","seventy","eighty","ninety"
  • Split into tens and ones
  • digits2 n (n div 10, n mod 10)
  • ? digits 76
  • (7,6)
  • ? digits2 345
  • (34,5)

10
Two Digits Solution (cont.)
  • Split into tens and ones then combine in correct
    way.
  • First a helper function
  • nth (l,n) l !! n
  • ? nth(1,2,3,4, 2)
  • 3
  • Now combine - Good only for two
    digit numbers -
  • combine2 (0,0) ""
  • combine2 (0,u) nth(units,u-1)
  • combine2 (1,u) nth(teens,u)
  • combine2 (t,0) nth(tens,t-2)
  • combine2 (t,u) (nth(tens,t-2)) "-"
    (nth(units,u-1))
  • convert2 combine2 . digits2
  • ? convert2 34
  • thirty-four

11
Three Digit Solution
  • Split into hundreds and tens
  • digits3 n (n div 100,n mod 100)
  • Then Combine
  • combine3 (0,t) convert2(t)
  • combine3 (h,0)
  • (nth(units,h-1)) " hundred"
  • combine3 (h,t)
  • (nth(units,h-1)) " hundred and "
  • (convert2 t)
  • convert3 combine3 . digits3
  • ? convert3 345
  • three hundred and forty-five
  • What about thousands ?

12
Sorting
  • Insertion Sort
  • Assume list is already sorted
  • insert item item
  • insert item (ax)
  • if item lt a
  • then item a x
  • else a (insert item x)
  • ? insert 5 1,3,6,8
  • 1,3,5,6,8
  • Now insert each element
  • sort
  • sort (xxs) insert x (sort xs)

13
Does this pattern look familiar?
  • sort
  • sort (xxs) insert x (sort xs)
  • foldr op e e
  • foldr op e (xxs) op x (foldr op e xs)
  • sort foldr insert

14
Sorting (cont)
  • Here is a definition of quiksort
  • First a helper function
  • non (a -gt Bool) -gt a -gt Bool
  • non f x not (f x)
  • Divide into two sublists and conquer
  • quiksort Ord a gt a -gt a
  • quiksort
  • quiksort (xxs)
  • (quiksort (filter (ltx) xs))
  • (x (quiksort (filter (non (ltx)) xs)))
  • ? quiksort 4,8,2,6,1,9
  • 1, 2, 4, 6, 8, 9

15
Better Quiksort
  • Can we take the two lists computed by two calls
    to filter and write a function which computes
    them both in one pass?
  • split Ord a gt a -gt a -gt (a,a)
  • split pivot (,)
  • split pivot (xxs)
  • let (sm,bg) split pivot xs
  • in if x gt pivot
  • then (sm,xbg)
  • else (xsm,bg)
  • ? split 4 1,2,3,4,5,6,7
  • (1, 2, 3,4, 5, 6, 7)
  • quik2
  • quik2 (xxs) (quik2 l) (x (quik2 h))
  • where (l,h) split x xs

16
Making Change
  • Producing Change (exact number of nickels,
    dimes, quarters, pennies) for a given amount.
  • Lots of solutions, how should we constrain the
    problem?
  • largest amount of "big" coins"
  • change x makechange x coins
  • where coins ("quarters",25), ("dimes",10),
  • ("nickels",5), ("pennies",1)
  • makechange x
  • makechange x ((word,amt)m)
  • let count x div amt
  • left x mod amt
  • in if count0
  • then makechange left m
  • else (count,word)(makechange
    left m)
  • ? change 72
  • (2,"quarters"), (2,"dimes"), (2,"pennies")
  • (50 reductions, 198 cells)
Write a Comment
User Comments (0)
About PowerShow.com