5 Words and Sentences - People
[Pages:14]5 Words and Sentences
We started out, in Part I, with examples about acronyms and so on, but since then we've been working with numbery old numbers. That's because the discussions about evaluation and procedure definition were complicated enough without introducing extra ideas at the same time. But now we're ready to get back to symbolic programming.
As we mentioned in Chapter 3, everything that you type into Scheme is evaluated and the resulting value is printed out. Let's say you want to use "square" as a word in your program. For example, you want your program to solve the problem, "Give me an adjective that describes Barry Manilow." If you just type square into Scheme, you will find out that square is a procedure:
> square #
(Different versions of Scheme will have different ways of printing out procedures.) What you need is a way to say that you want to use the word "square" itself, rather
than the value of that word, as an expression. The way to do this is to use quote:
> (quote square) SQUARE > (quote (tomorrow never knows)) (TOMORROW NEVER KNOWS) > (quote (things we said today)) (THINGS WE SAID TODAY)
57
Quote is a special form, since its argument isn't evaluated. Instead, it just returns the argument as is.
Scheme programmers use quote a lot, so there is an abbreviation for it:
> 'square SQUARE
> '(old brown shoe) (old brown shoe)
(Since Scheme uses the apostrophe as an abbreviation for quote, you can't use one as an ordinary punctuation mark in a sentence. That's why we've been avoiding titles like (can't buy me love). To Scheme this would mean (can (quote t) buy me love)!)*
This idea of quoting, although it may seem arbitrary in the context of computer programming, is actually quite familiar from ordinary English. What is a book? It's a bunch of pieces of paper, with printing on them, bound together. What is "a book"? It's a noun phrase, made up of an article and a noun. See? Similarly, what's 2 + 3? It's five. What's "2 + 3"? It's an arithmetic formula. When you see words inside quotation marks, you understand that you're supposed to think about the words themselves; you don't evaluate what they mean. Scheme is the same way.
(It's no accident that kids who make jokes like
Matt: "Say your name."
Brian: "Your name."
grow up to be computer programmers. The difference between a thing and its name is one of the important ideas that programmers need to understand.)
* Actually, it is possible to put punctuation inside words as long as the entire word is enclosed in double-quote marks, like this:
> '("can't" buy me love) ("can't" BUY ME LOVE)
Words like that are called strings. We're not going to use them in any examples until almost the end of the book. Stay away from punctuation and you won't get in trouble. However, question marks and exclamation points are okay. (Ordinar y words, the ones that are neither strings nor numbers, are officially called symbols.)
58
Part II Composition of Functions
Selectors
So far all we've done with words and sentences is quote them. To do more interesting work, we need tools for two kinds of operations: We have to be able to take them apart, and we have to be able to put them together.* We'll start with the take-apart tools; the technical term for them is selectors.
> (first 'something) S
> (first '(eight days a week)) EIGHT
> (first 910) 9
> (last 'something) G
> (last '(eight days a week)) WEEK
> (last 910) 0
> (butfirst 'something) OMETHING
> (butfirst '(eight days a week)) (DAYS A WEEK)
> (butfirst 910) 10
> (butlast 'something) SOMETHIN
* The procedures we're about to show you are not part of standard, official Scheme. Scheme does provide ways to do these things, but the regular ways are somewhat more complicated and error-prone for beginners. We've provided a simpler way to do symbolic computing, using ideas developed as part of the Logo programming language.
Chapter 5 Words and Sentences
59
> (butlast '(eight days a week)) (EIGHT DAYS A)
> (butlast 910) 91
Notice that the first of a sentence is a word, while the first of a word is a letter. (But there's no separate data type called "letter"; a letter is the same as a one-letter word.) The butfirst of a sentence is a sentence, and the butfirst of a word is a word. The corresponding rules hold for last and butlast.
The names butfirst and butlast aren't meant to describe ways to sled; they abbreviate "all but the first" and "all but the last."
You may be wondering why we're given ways to find the first and last elements but not the 42nd element. It turns out that the ones we have are enough, since we can use these primitive selectors to define others:
(define (second thing) (first (butfirst thing)))
> (second '(like dreamers do)) DREAMERS
> (second 'michelle) I
There is, however, a primitive selector item that takes two arguments, a number n and a word or sentence, and returns the nth element of the second argument.
> (item 4 '(being for the benefit of mister kite!)) BENEFIT
> (item 4 'benefit) E
Don't forget that a sentence containing exactly one word is different from the word itself, and selectors operate on the two differently:
> (first 'because) B
> (first '(because)) BECAUSE
60
Part II Composition of Functions
> (butfirst 'because) ECAUSE
> (butfirst '(because)) ()
The value of that last expression is the empty sentence. You can tell it's a sentence because of the parentheses, and you can tell it's empty because there's nothing between them.
> (butfirst 'a) ""
> (butfirst 1024) "024"
As these examples show, sometimes butfirst returns a word that has to have doublequote marks around it. The first example shows the empty word, while the second shows a number that's not in its ordinary form. (Its numeric value is 24, but you don't usually see a zero in front.)
> 024 24
> "024" "024"
We're going to try to avoid printing these funny words. But don't be surprised if you see one as the return value from one of the selectors for words. (Notice that you don't have to put a single quote in front of the double quotes. Strings are self-evaluating, just as numbers are.)
Since butfirst and butlast are so hard to type, there are abbreviations bf and bl. You can figure out which is which.
Constructors
Functions for putting things together are called constructors. For now, we just have two of them: word and sentence. Word takes any number of words as arguments and joins them all together into one humongous word:
> (word 'ses 'qui 'pe 'da 'lian 'ism) SESQUIPEDALIANISM
Chapter 5 Words and Sentences
61
> (word 'now 'here) NOWHERE
> (word 35 893) 35893
Sentence is similar, but slightly different, since it can take both words and sentences as arguments:
> (sentence 'carry 'that 'weight) (CARRY THAT WEIGHT)
> (sentence '(john paul) '(george ringo)) (JOHN PAUL GEORGE RINGO)
Sentence is also too hard to type, so there's the abbreviation se.
> (se '(one plus one) 'makes 2) (ONE PLUS ONE MAKES 2)
By the way, why did we have to quote makes in the last example, but not 2? It's because numbers are self-evaluating, as we said in Chapter 3. We have to quote makes because otherwise Scheme would look for something named makes instead of using the word itself. But numbers can't be the names of things; they represent themselves. (In fact, you could quote the 2 and it wouldn't make any difference--do you see why?)
First-Class Words and Sentences
If Scheme isn't your first programming language, you're probably accustomed to dealing with English text on a computer quite differently. Many other languages treat a sentence, for example, as simply a collection (a "string") of characters such as letters, spaces, and punctuation. Those languages don't help you maintain the two-level nature of English text, in which a sentence is composed of words, and a word is composed of letters.
Historically, computers just dealt with numbers. You could add two numbers, move a number from one place in the computer's memory to another place, and so on. Since each instruction in the computer's native machine language couldn't process anything larger than a number, programmers developed the attitude that a single number is a "real thing" while anything more complicated has to be considered as a collection of things, rather than as a single thing in itself.
62
Part II Composition of Functions
The computer represents a text character as a single number. In many programming languages, therefore, a character is a "real thing," but a word or sentence is understood only as a collection of these character-code numbers.
But this isn't the way in which human beings normally think about their own language. To you, a word isn't primarily a string of characters (although it may temporarily seem like one if you're competing in a spelling bee). It's more like a single unit of meaning. Similarly, a sentence is a linguistic structure whose parts are words, not letters and spaces.
A programming language should let you express your ideas in terms that match your way of thinking, not the computer's way. Technically, we say that words and sentences should be first-class data in our language. This means that a sentence, for example, can be an argument to a procedure; it can be the value returned by a procedure; we can give it a name; and we can build aggregates whose elements are sentences. So far we've seen how to do the first two of these. We'll finish the job in Chapter 7 (on variables) and Chapter 17 (on lists).
Pitfalls
We've been avoiding apostrophes in our words and sentences because they're abbreviations for the quote special form. You must also avoid periods, commas, semicolons, quotation marks, vertical bars, and, of course, parentheses, since all of these have special meanings in Scheme. You may, however, use question marks and exclamation points.
Although we've already mentioned the need to avoid names of primitives when choosing formal parameters, we want to remind you specifically about the names word and sentence. These are often very tempting formal parameters, because many procedures have words or sentences as their domains. Unfortunately, if you choose these names for parameters, you won't be able to use the corresponding procedures within your definition.
(define (plural word) (word word 's))
;; wrong!
> (plural 'george) ERROR: GEORGE isn't a procedure
The result of substitution was not, as you might think,
(word 'george 's)
Chapter 5 Words and Sentences
63
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- letters starter course unit 7 section 5
- stage 2 three letter words with short vowel sounds
- using the letters of the alphabet rackspace
- 5 words and sentences people
- word games american english
- types of letters towson university
- coe808 lab 5 python scrabble ryerson university
- 6 00 introduction to computer science and programming fall
- qumran letter restoration by rotation and reflection modified
- critical components in early literacy — knowledge of the letters
Related searches
- definitions and sentences dictionary
- words and sentences in spanish
- 5 hard words and definitions
- use words in sentences website
- words to describe people s character
- words for a people person
- definition and sentences dictionary
- words that mean people person
- words that describe people s personality
- words to describe people s characteristics
- identifying phrases and sentences worksheets
- other words for a people person