Download presentation
Presentation is loading. Please wait.
1
Cs536 Functional Programming 16/13/2015 Lecture #2.5 Today’s 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
Cs536 Functional Programming 26/13/2015 Numerical Functions Differentiating a function diff :: (Float -> Float) -> (Float -> Float) diff f = f' where f' x = (f (x+h) - f x)/ h h = 0.0001 For Small enough h, good approximation of slope of f h x f f x f(x+h) f x - f(x+h)
3
Cs536 Functional Programming 36/13/2015 Square Root Again! Repetition on functions until :: (a -> Bool) -> (a -> a) -> a -> a ? until (>10) (+1) 1 11 ? until (< 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)<h && abs(b-a)<h where h = 0.00001 Square Root sqrRoot n = until goodenough (next n) 1.0 where goodenough p = p*p ~= n
4
Cs536 Functional Programming 46/13/2015 Lists of Prime Numbers Does one number evenly divide another? divisible :: Int -> Int -> Bool divisible t n = t `rem` n == 0 denominators :: Int -> [Int] denominators x = filter (divisible x) [1..x] ? denominators 235 [1, 5, 47, 235] A Prime has only two denominators prime :: Int -> Bool prime x = denominators x == [1,x] ? prime 12 False ? prime 37 True
5
Cs536 Functional Programming 56/13/2015 Primes (cont.) To get a list of primes then generate and prune. primes :: Int -> [ 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
Cs536 Functional Programming 66/13/2015 A List Display Tool How do we print? [1,2,3,4] pl :: Text a => [a] -> [Char] pl [] = "" pl (x:xs) = (show x) ++ "," ++ (pl xs) ? pl [1,2] 1,2, How do we get rid of the annoying last "," pl :: Text a => [a] -> [Char] pl [] = "" pl [x] = (show x) pl (x:xs) = (show x) ++ "," ++ (pl xs) ? pl [1,2,3] 1,2,3
7
Cs536 Functional Programming 76/13/2015 List Display Tool (cont.) What about the “[“ and “]” pl :: Text a => [a] -> [Char] pl l = "[" ++ (p l) ++ "]" where p [] = "" p [x] = (show x) p (x:xs) = (show x) ++ "," ++ (p xs) ? pl [1,2,3,4] [1,2,3,4] ? pl [True, not True] [True,False]
8
Cs536 Functional Programming 86/13/2015 Generalize let “[“ and “]” be parameters pl :: Text a => [Char] -> [Char] -> [Char] -> [a] -> [Char] pl front back sep l = front ++ (p l) ++ back where p [] = "" p [x] = (show x) p (x:xs) = (show x) ++ sep ++ (p xs) ? pl "{" "}" " " [1,2,3] {1 2 3} ? pl "(" ")" "," [1,2,3] (1,2,3)
9
Cs536 Functional Programming 96/13/2015 Writing out numbers in long hand 342 -> 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","fourteen", "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
Cs536 Functional Programming 106/13/2015 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
Cs536 Functional Programming 116/13/2015 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
Cs536 Functional Programming 126/13/2015 Sorting Insertion Sort –Assume list is already sorted insert item [] = [item] insert item (a:x) = if item < 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 (x:xs) = insert x (sort xs)
13
Cs536 Functional Programming 136/13/2015 Does this pattern look familiar? sort [] = [] sort (x:xs) = insert x (sort xs) foldr op e [] = e foldr op e (x:xs) = op x (foldr op e xs) sort = foldr insert []
14
Cs536 Functional Programming 146/13/2015 Sorting (cont) Here is a definition of quiksort: First a helper function non :: (a -> Bool) -> a -> Bool non f x = not (f x) Divide into two sublists and conquer quiksort :: Ord a => [a] -> [a] quiksort [] = [] quiksort (x:xs) = (quiksort (filter (<=x) xs)) ++ (x : (quiksort (filter (non (<=x)) xs))) ? quiksort [4,8,2,6,1,9] [1, 2, 4, 6, 8, 9]
15
Cs536 Functional Programming 156/13/2015 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 => a -> [a] -> ([a],[a]) split pivot [] = ([],[]) split pivot (x:xs) = let (sm,bg) =split pivot xs in if x >= pivot then (sm,x:bg) else (x:sm,bg) ? split 4 [1,2,3,4,5,6,7] ([1, 2, 3],[4, 5, 6, 7]) quik2 [] = [] quik2 (x:xs) = (quik2 l) ++ (x : (quik2 h)) where (l,h) = split x xs
16
Cs536 Functional Programming 166/13/2015 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 count==0 then makechange left m else (count,word):(makechange left m) ? change 72 [(2,"quarters"), (2,"dimes"), (2,"pennies")] (50 reductions, 198 cells)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.