4 Working with words and sentences | (9 activities) |
1.Define a procedure named geometric-mean that, given two numbers as arguments, returns the square root of their product. You will probably want to use the sqrt procedure, which takes the square root of a number. |
2.Consider the following procedure:
(define (f a b) (* b (+ 3 a)) )Determine the value of the expression (f (f 3 4) 5) |
3.Assume that a definition of square is written:
(define (square x) (* x x))Write a call to the procedure square with the argument 3. |
4.Write a call to the procedure square that will result in the error:
"too few arguments to..." |
5.Write a call to the procedure square that will result in the error:
"too many arguments to..." |
6.Write a call to the procedure square that will result in the error:
"not a number..." |
Review some builtin procedures that take words as inputs.Up to now, we've worked only with procedures that take numbers as inputs (arguments) and return numbers as values. As you learned from yesterday's reading assignment, the Scheme interpreter also has builtin procedures that work with words and sentences. We'll initially start experimenting with words and then move on to sentences. Two builtin procedures of interest are
|
Experiment in the Scheme interpreter with the first procedure.
You will find that
(first mike)produces an error, while (first (quote mike))returns an "m". Explain why evaluating (first mike) produces an error message. |
Why didn't we need to use quote with numbers?Numbers in Scheme are self-evaluating; that is, they evaluate to themselves. This is why you can say(+ 1 2 3 4)instead of (+ (quote 1) (quote 2) (quote 3) (quote 4))It's OK to quote numbers if you want, but not necessary. |
Now type the following procedures into the Scheme interpreter.
(define (initial1 name) (first name) ) (define (initial2 name) (first (quote name)) )Try to use each procedure to find Mike's first initial, by supplying the word miketo each as an argument. That is, what happens when you call (initial1 'mike)and (initial2 'mike)Explain what each procedure is doing, and why. |
Now try it with the word procedure.The word builtin procedure takes any number of words as arguments (it's like the + and * procedures, which take any number of numbers as arguments), and returns the result of gluing all the words together into a single word. Here's an example:>(word (quote i) (quote am) (quote bic)) iambicWrite and test a procedure named plural that returns the result of gluing an "s" onto the end of its argument. Put your procedure into a file named plural.scm. Here are examples of how plural would be used. >(plural (quote house)) houses >(plural (quote guess)) guesss |
Here's an abbreviation for quote.The quoting operation is used so often that Scheme provides an abbreviation for quote: the expression'xis translated by the Scheme interpreter into the expression (quote x)for any x. |