1 / 8

Programming Paradigms

Programming Paradigms. CPSC 449 Week 2-2 Prepared by : Mona Hosseinkhani, Arash Afshar Winter 2014. Department of Computer Science, University of Calgary. Local Definitions. Solving a problem in steps: local definitions as a part of function definition Lets start with an example:

hide
Télécharger la présentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms CPSC 449 Week 2-2 Prepared by : Mona Hosseinkhani, ArashAfshar Winter 2014 Department of Computer Science, University of Calgary

  2. Local Definitions • Solving a problem in steps: local definitions as a part of function definition • Lets start with an example: • Define a function: f

  3. Local Definitions fourPics1 :: Picture -> Picture fourPics1 pic = left `beside` right where left = … right = … • One way to tackle: Local Definitions fourPics1 :: Picture -> Picture fourPics pic = left `beside` right where left = pic `above` invertColour pic right = invertColour (flipV pic) `above` flipV pic

  4. Calculation with local definitions • The values of the local definitions are calculated beneath the where if their values are needed • Example: sumSquares :: Integer -> Integer -> Integer sumSquares n m = sqN + sqM where sqN = n*n sqM = m*m • Consider the indentation (offside rule) for local definitions after where

  5. let expressions • For making definitions local to an expression vs. functions let x = 3+2 in x^2 + 2*x - 4 letEx1 :: Integer letEx1 = let x = 3+2 in x^2 + 2*x - 4 letEx2 :: Integer letEx2 = let x = 3+2 ; y = 5-1 in x^2 + 2*x - y

  6. Local definitions scopes • Scope of definition: part of the program in which the definition can be used • Example: (Visible in whole code) isOdd, isEven :: Int -> Bool isOdd n | n<=0 = False | otherwise = isEven (n-1) isEven n | n<0 = False | n==0 = True | otherwise = isOdd (n-1)

  7. Local definitions scopes • Local definitions scope: visible in the conditional equation in which they appear • Local definitions can be used before they are defined • Example:

  8. Questions hossem@ucalgary.ca http://www.ucalgary.ca/mhosseinkhani/CPSC449

More Related