S04ProofModuleGrapics

# S04ProofModuleGrapics - Cse536 Functional Programming...

This preview shows pages 1–12. Sign up to view the full content.

Cse536 Functional Programming 1 10/05/15 Lecture #4, Oct 6, 2004 Reading Assignments Begin Reading chapter 3 (Simple Graphics) of the Text Homework Assignment 2 exercises 2.1 and 2.2 pp. 25 of the text exercises 2.5 pp. 33 of the text Today’s Topics Home work solutions to assignment #1 Program Manipulation Comparing the functional paradigm Actions and Haskell Monads Simple Graphics

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cse536 Functional Programming 2 10/05/15 Home work solutions First Recursively Second by combining functions Write a function: strlen which returns the length of a string. e.g. strlen "abc" --> 3 strlen "" --> 0 strlen :: String -> Int strlen [] = 0 strlen (x:xs) = 1 + strlen xs strlenA = length strlenB = sum . map (const 1) strlenC = foldr (+) 0 . map (const 1)
Cse536 Functional Programming 3 10/05/15 Factorial Write a function which computes n factorial. E.g. fact 0 --> 1 fact 3 --> 6 fact 5 --> 120 factA 0 = 1 factA n = n * (factA (n-1)) factB n = product [1..n]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cse536 Functional Programming 4 10/05/15 ncopies Write the ncopies function. For example: ncopies 3 5 --> [5,5,5] ncopies 4 "a" --> ["a","a","a","a"] ncopies 0 True --> [] ncopiesA :: Num a => a -> b -> [b] ncopiesA 0 x = [] ncopiesA n x = x : (ncopiesA (n-1) x) ncopiesB n x = map (const x) [1..n] ncopiesC n x = [ x | y <- [1..n] ] ncopiesD n x = take n (repeat x)
Cse536 Functional Programming 5 10/05/15 Power Write the power function for integers. For example: power 5 2 --> 25 power 3 3 --> 27 power 2 5 --> 32 powerA x 0 = 1 powerA x 1 = x powerA x n = x * (powerA x (n-1)) powerB x n = product (ncopies n x)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cse536 Functional Programming 6 10/05/15 String to Integer 7) Write a function which converts a string of digits into an Int. You will need the following predefined function: ord ‘1’ --> 49 Char to its ascii code follow the following "pipeline" analysis when defining your function "167" --> ['1','6','7'] --> [49,54,55] --> [1,6,7] --> [(1,100),(6,10),(7,1)] --> [100, 60, 7] --> 167 (hint: the first function in the pipeline is very simple. why?)
Cse536 Functional Programming 7 10/05/15 String to Int (cont) Then str2int is an easy composition str2int :: String -> Int str2int = sum . map (uncurry (*)) . explist . map (\ z -> z -(ord '0')) . map ord The Key is the function explist explist [5,3,4] --> [(5,100),(3,10),(4,1)] How many lists are traversed?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cse536 Functional Programming 8 10/05/15 Key Function Explist Useful intermediates reverse [1,10,100] [1,10,100] --> [100,10,1] zip [3,4] [100, 10, 1] --> [(3,100), (4,10)] Definition explist zs = zip zs [ power 10 x | x <- reverse [0 .. n-1] ] where n = length zs
Cse536 Functional Programming 9 10/05/15 Another explist explist = fst . foldr g ([],1) where z `g` (zs,n) = ((z,n) : zs, n * 10) Suppose we start with [5,3,2] Folding g leaves 5 `g` (3 `g` (2 `g` ([],1))) 5 `g` (3 `g` ((2,1):[],10)) 5 `g` (3 `g` ([(2,1)],10)) 5 `g` ((3,10):[(2,1)],100) 5 `g` ([(3,10),(2,1)],100) ([(5,100),(3,10),(2,1)],1000)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Cse536 Functional Programming 10 10/05/15 Program Manipulation Substitution of equals for equals. if name: f x = e is a definition or a theorem, then we can replace ( f n) with e[n/x] wherever ( f n ) occurs . name: is the name of the definition or theorem for reference in the proof. e[n/x] means e with all free occurrences of x replaced by n For example consider: comp: (f . g) x = f (g x) Now prove that ((f . g) . h) x = (f . (g . h)) x
Cse536 Functional Programming 11 10/05/15 Proof Pick one side of the equation and transform using rule comp: above ((f . g) . h) x = by comp: (left to right) (f . g) (h x) = by comp:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern