Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 A preview… A Scheme program includes one or more functions, defined using the rule: (define (function-name parameter-names) (expression)) A Scheme program may also includes one or more variables, defined using the rule: (define VARIABLE-NAME its-value)

3 REVIEW: Meaning of the Function Definition In the function: (define (square r) (* r r)) we tell Scheme that: (square r) is the same thing as typing: (* r r)

4 Example 1: area-of-disk Suppose we want to write a function to find the area of a circle. The formula is A = PI * r 2 We could write: (define (area-of-disk r) (* PI (* r r))) However, notice that (* r r) is actually just the function body of the square function.

5 Example 1: area-of-disk So we can instead write: (define (area-of-disk r) (* PI (square r))) replacing (* r r) with (square r). Even if we named our parameter something different in the square function, it is still allowed.

6 Example 1: area-of-disk Then, we have: (define (square num) (* num num)) (define (area-of-disk r) (* PI (square r)) Since our goal here is to write area-of- disk, we call that the main function. Then, square which helps us write area-of-disk, is called an auxiliary function.

7 Example 2: checkerboard The checkerboard function can be written as: (define (checkerboard color1 color2) (image-above (image-beside (rectangle 50 50 ‘solid color1) (rectangle 50 50 ‘solid color2) ) (image-beside (rectangle 50 50 ‘solid color2) (rectangle 50 50 ‘solid color1) )

8 Example 2: checkerboard The program can more succinctly (and clearly) be written using function composition: Since we are using the following setup, (image-above (image-beside ____ _____) ) we can write a separate function for that part.

9 Example 2: checkerboard ;Auxiliary function: (define (counterchange img1 img2) (image-above (image-beside img1 img2) (image-beside img2 img1) )

10 Example 2: checkerboard ; This allows the checkerboard function to be ; written in this shorter way. ;Main function: (define (checkerboard color1 color2) (counterchange (rectangle 50 50 ‘solid color1) (rectangle 50 50 ‘solid color2) )

11 Writing Multi-Function Programs While developing a Scheme function, you may realize the need or benefit of an auxiliary function. One example is when you have a setup of several functions like image-above and image- beside. You can create a separate function that incorporates those three. We called that separate function counterchange. Another example is when you have the same or similar code twice in a single function. Then you can write an auxiliary function that addresses just the part of the function with that same or similar code.

12 Writing Multi-Function Programs Sometimes the auxiliary function is predefined, like + or first. If the auxiliary function is not predefined, you will have to define it yourself. You should pause the designing of your main function, while you complete a separate Design Recipe for the auxiliary function(s). Then complete the Design Recipe for the main function.

13 CW3: For Section 02 only (the 4:30 class) Develop a Scheme function called add-question-mark that takes in a sentence and returns the same sentence with a question mark at the end of the last word. Develop a Scheme function called add-question-mark that takes in a sentence and returns the same sentence with a question mark at the end of the last word. Submit the Definitions Window ONLY using the Digital Dropbox on Blackboard with the file name: “CW3-02[LastName]Def.scm” Submit the Definitions Window ONLY using the Digital Dropbox on Blackboard with the file name: “CW3-02[LastName]Def.scm”

14 Exercise 5.21: query Develop a Scheme function, query, that turns a statement into a question by swapping the first two words and adding a question mark to the last word. Develop a Scheme function, query, that turns a statement into a question by swapping the first two words and adding a question mark to the last word. Example: Example: ; (query ‘(you are experienced)) “should be” ; (ARE YOU EXPERIENCED?)

15 Preparing for Next Time…

16 In summary… When there are multiple things that need to happen in a program, write them as separate functions. When the auxiliary function you want to use is not predefined, define your own. Whenever developing a function that requires auxiliary functions, complete the steps of the Design Recipe separately for every function you write.

17 Next time… More on variables If you haven’t already, please read the following before next class: –Simply Scheme, pages 89-93. –How to Design Programs, section 3.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google