Study Guides (238,528)
Canada (115,195)
CS 116 (15)
Jiye Li (1)

Lecture Notes + Examples Lecture notes and examples

14 Pages
Unlock Document

University of Waterloo
Computer Science
CS 116
Jiye Li

CS 116 Introduction to Computer ScienceModule 1 ReviewDesign Recipe A development process that leaves behind a written explanation1ContractDescribes what type of arguments the function consumes and what value it produces2PurposeDescribes what the program is to compute ie consumesproduces3ExamplesIllustrating use of the program4DefinitionScheme definition of the function 5TestsA representative set of inputs and expected outputsex sumofsquares num numnum Purpose produces sum of squares of arg1 and arg2 Examples sumof squares 0 25625 sumofsquares 3 425define sumofsquares arg1 arg2 arg1 arg1arg2 arg2 Test sumofsquarescheckexpect sumofsquares 2 3 13checkexpect sumofsquares 1 1 2 checkexpect is used for exact numbers only use checkwithin for a range of valuesStructure Combines a fixed number of values into a single piece of data represents compounds of information1 Structure Definition definestruct nameofstructure var1 var2vark2 Data Definition comment on the constraints of each var3Regular design recipe iContract instead of num symbol string etc use the name of the structurefor the use of one type of variable use union type1 type2etcexmminfoartist union mp3info movieinfostringmminfoartist makemp3info Beck Time Bomb 183 RockBeckmminfoartist makemovieinfo Orson Welles Citizen Kane 119 ClassicOrson WellesiiPurposeiiiExamplesivTestingCreate structure define structure1 makenameofstructure n1 n2nkExtract a single item from structure using selector functions nameofstructurevar1 structure1n1nameofstructurevar2 structure1n2 nameofstructurevark structure1nk1Ex definestruct date year month day hour A date is a structure makedate y m d h where y is a positive integer2009 representing the year m a month in a year is an integer in the range of 1 to 12 inclusive d a day in a month is an integer in the range of 1 to 31 inclusive h is an integer in the range of 0 to 23 inclusive to represent an hour in a daydefine date1 makedate 2001 6 2 23dateyear date12001datemonth date16dateday date12datehour date123local contains a series of local definitions variables helper functions constants etc plus an expression evaluation using them of the form local def1 def2expModule 2 More Functional Abstractionfilter consumes a predicate and a list and filters out all items in that list that produce true to the predicate filter Xboolean listof Xlistof Xdefine filter pred alistcondempty alist emptypred first alist cons first alist filter pred rest alistelse filter pred rest alistEx filter even 1 2 3 4 5 72 4map transforms a list elementbyelement into another list of the same length map XY listof Xlistof Ydefine map f alistcondempty alist emptyelse cons f first alist map f rest alistEx map sqr 2 3 44 9 16foldr consumes a list of X and produces a Y by processing the list to the right foldr X XX X listof XXdefine foldr combine base alistcondempty alist baseelse combine first alistfoldr combine base rest alistEx foldr0 1 2 36 foldr max 0 2 4 6 882
More Less

Related notes for CS 116

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.