Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 A preview… Last week, we saw how to (and how not to) define functions using several examples involving numbers. Today, we will apply the same rule for defining functions to words and sentences.

3 Functions without Numbers Here is a function that takes a U.S. state as its argument. Alabama -> Montgomery Arkansas -> Little Rock California -> Sacramento What does this function do?

4 Functions without Numbers Here is a function that takes a U.S. state as its argument. Alabama -> Montgomery Arkansas -> Little Rock California -> Sacramento What does this function do? It returns the capital of a given state.

5 Recall the following… Last time, we discussed not using function names as parameter names, as in the following illegal example: (define (area square) (square square)) Also, recall that word and sentence are not only data types, but also the names of two predefined functions.

6 Words of Caution It is often very tempting to use word or sentence as a parameter name. Do not do this! Why not? Because, then you cannot use the word or sentence functions when defining your function. This is the same reason we could not use the square function when square was a parameter.

7 Example 1: plural Define a Scheme function that pluralizes a word by adding an “s”. Assume for now that all English words can be made plural simply by adding “s” (even though we know this is not the case). Be sure to write an example in Scheme notation with its expected answer BEFORE defining the function.

8 Solution ; (plural ‘cat) “should be” cats ; func-name = plural ; param-name = n ; expression = (word n ‘s) (define (plural n) (word n ‘s)) ;As always, after defining a function, make sure ;your example returns the answer that is expected.

9 Example 2: second Define a Scheme function second that selects any of the following: --the second letter of a word --the second word of a sentence --the second digit of a number. Start by writing examples for each case: ;(second ‘hello) “should be” ‘e ;(second ‘(it is Tuesday)) “should be” ‘is ;(second 10306) “should be” 0

10 Solution ; func-name = second ; param-name = whole ; expression = (item 2 whole) (define (second whole) (item 2 whole))

11 CW3: For Section 01 only (the 3:05 class) In Simply Scheme mode: Define a Scheme function greet that takes in a single name (like ‘adam) and says hello to it. Define a Scheme function greet that takes in a single name (like ‘adam) and says hello to it. EXTRA CREDIT: Define a Scheme function greet2 that takes in a full name (like ‘adam ‘wittenstein) and says hello to it. EXTRA CREDIT: Define a Scheme function greet2 that takes in a full name (like ‘adam ‘wittenstein) and says hello to it. Be sure to write an example in Scheme notation, before defining each of the functions. Be sure to write an example in Scheme notation, before defining each of the functions. Submit using the Digital Dropbox on Blackboard with the following file names: Submit using the Digital Dropbox on Blackboard with the following file names: -- CW3-01[LastName]Def.scm

12 In summary… Regardless of the data type, we can define functions (and write examples) in exactly the same way. We will define functions with images and booleans in the coming weeks. Example 2 (and CW3) show that you can even involve multiple data types at the same time, as long as you are careful to follows the rules of any (predefined) functions you are using. Read How to Design Programs section 2.5 BEFORE our next class.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google