Download presentation
Presentation is loading. Please wait.
1
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/
2
A preview… Last time we called numeric functions, like +, -, *, /, and sqrt. Today, we will see that Scheme treats other types of data, like words and sentences, in the same way that it treats numbers. Again, we have not learned how to define our own functions yet, we will just call predefined functions today, knowing that user-defined functions are called in exactly the same way.
3
I. The Stepper
4
Using the stepper If you are not getting the answers that you expect, then use DrScheme’s Stepper tool. You need to change the “language” to How to Design Programs Beginner Student. It shows you how a complex expression is evaluated, step-by-step. For example, take the expression (+ 2 (* 3 4)) Just as with PEMDAS, DrScheme must evaluate the (* 3 4) expression first to get 12. Then, it can evaluate (+ 2 12) to get 14.
5
Using the stepper To use the Stepper tool, put the expression(s) you want to evaluate into DrScheme's top area, the definitions window, then click the Step button:
6
Using the stepper Click next and you will get the answer of 14.
7
Example 2: Name the little people From page 35 in the Simply Scheme textbook, (+ (* 3 (sqrt 49) (/ 12 4))) First write it on several lines, (+ (* 3 (sqrt 49) (/ 12 4) ) ) ;Error: You cannot add a single number.
8
Example 3: Name the little people Similar to page 35 in the Simply Scheme textbook, (+ (* 3 (sqrt 49) (/ 12 4) 1)) Then assign names, (+ Adam (* 3Brian (sqrt 49)Christina (/ 12 4)Dawn ) 1)
9
Example 3: The little people get to work Christina: (sqrt 49) = 7 Dawn: (/ 12 4) = 3 Brian: (* 3 7 3) = 63 Adam: (+ 63 1) = 64
10
Example 4: Step through an expression Expression:(- ( + 5 8) (+ 2 4)) Step 1 (Bernie):(- 13 (+ 2 4)) Step 2 (Cordelia):(- 13 6) Step 3 (Alice):7
11
II. The “Quote” Function
12
Example 1: quote To print out a word, use the quote function. For example, (quote square) should return SQUARE. What will (quote (class on thursday)) return?
13
Example 1: quote To print out a word, use the quote function. For example, (quote square) should return square. What will (quote (class on thursday)) return? It returns class on thursday. An abbreviation: (quote square) can also be written as ‘square.
14
Why bother with quoting? While it seems nonsensical here, its actually common in English. For example, compare the following: -- a book is a bunch of pieces of paper. -- “a book” is an article and a noun Also, compare the following: -- 2 + 3 is 5. -- “2+3” is an arithmetic expression.
15
Why bother with quoting? When you see words inside quotation marks, think about the word itself, rather than its meaning. When the word is not in quotation marks, think about its actual meaning. This difference between a thing and its name is fundamental to computer programming.
16
III. Selector and Constructor Functions
17
Example 2: Last Week’s Functions Using the previous notes and readings: Calling a Selector Function Type something that takes the word hello and returns the first letter. Calling a Constructor Function Type something that takes two parts of a sentence and puts them together.
18
Example 3: Two “first” functions First returns the first letter of a word. For example, (first ‘because) = b. First also returns the first word in a sentence. For example, (first ‘(because)) = because. Notice the use of a ‘ and ( ) for a sentence.
19
Example 4: The “item” functions Just as with the first function, it works in two ways: 1) Nth letter in a word: (item 4 ‘Thursday) = R 2) Nth word in a sentence: (item 4 ‘(Today is Thursday September Seven)) = SEPTEMBER
20
Example 5: The sentence function There is one sentence function that takes just words, just sentences, or a combination of both and turns them into a sentence. Example of just words: (sentence ‘carry ‘that ‘weight) = (carry that weight)
21
Example 5: The sentence function Example of just sentences: (sentence ‘(john paul) ‘(george ringo)) = (john paul george ringo) Example of combining words & sentences: (sentence ‘(Today is Thursday) ‘the ‘seventh) = (Today is Thursday the seventh)
22
IV. First-Class Words and Sentences
23
In other programming languages… A word is a collection of letters and punctuation. A sentence is a collection of letters, punctuation, and spaces. Programmers call both of these strings. Scheme (and almost every other programming language) have such data types as numbers, strings, and booleans. However, we are actually using the Simply Scheme language, which is the Scheme language with some additions, like the data types for words and sentences.
24
What are objects? An object is a specific instance of a data type. Some objects of type number are: 5, -3, 4.7, ½. The only objects of type boolean are: true and false. Some objects of type word are: ‘hello & ‘a. Some objects of type sentence are: ‘(because) & ‘(Computers are fun!).
25
How can objects be used? In programming, a specific object can: 1)Be an argument to a function (also called procedure). For example, in (sqrt 49), the function is sqrt and the argument is our specific object, 49. 2)Be the value returned by a function. For example, (sqrt 49) returns the specific object called 7. 3)Be given a name. (We will see how in a few weeks.) 4)On compound data. (We will hopefully get to this at the end of the semester.)
26
Be very careful with punctuation We have defined the symbol ‘ as an abbreviation for quote. Scheme (and Simply Scheme) treats some English language punctuation the same way it would treat the letter g or the digit 5. However, some English language punctuation is also used as Scheme punctuation like ‘ and ( ). To avoid problems, we do not use any of this “overlapping punctuation”, even when using functions like word and sentence that apply to the English language.
27
V. Preparing for Next Time
28
A word of caution… Unless otherwise noted, we will use the Simply Scheme “language”. When using the Stepper though, we switch to the Beginner Student “language”. Two differences between these: -- A sentence is written with ‘(…) in Simply Scheme but “…” in Beginner Student. -- In Beginner Student, symbols correspond to words and strings correspond to sentences.
29
In summary… Today, we saw that functions are called the same way regardless of what type the data is. We also defined objects and listed the 4 ways they are used in programming. Next time… We will call functions involving pictures. We will see how to define variables (and time permitting, functions).
30
Reading assignments past due Online Textbook –Preface (the parts given in class on Tuesday 9/5) –Section 1 –Section 2 (through first subsection) Simply Scheme –Forward and Preface –Chapter 1 (through page 7) –Chapters 2, 3, and 5 *No new reading assignments for next time.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.