This page intentionally left blank Python for Software Design


FORMAL AND NATURAL LANGUAGES Natural languages



tải về 1.38 Mb.
Chế độ xem pdf
trang7/83
Chuyển đổi dữ liệu13.08.2023
Kích1.38 Mb.
#55046
1   2   3   4   5   6   7   8   9   10   ...   83
- Python for Software Design How to Think Like a Computer Scientist-Cambridge University Press (2009)

1.4
FORMAL AND NATURAL LANGUAGES
Natural languages are the languages people speak, such as English, Spanish, and
French. They were not designed by people (although people try to impose some
order on them); they evolved naturally.
Formal languages are languages that are designed by people for specific applica-
tions. For example, the notation that mathematicians use is a formal language that is
particularly good at denoting relationships among numbers and symbols. Chemists
use a formal language to represent the chemical structure of molecules. And most
importantly:
Programming languages are formal languages that have been designed to express
computations.
Formal languages tend to have strict rules about syntax. For example, 3
+ 3 = 6
is a syntactically correct mathematical statement, but 3
+ = 3$6 is not. H
2
O is a
syntactically correct chemical formula, but
2
Zz is not.
Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the
basic elements of the language, such as words, numbers, and chemical elements. One
of the problems with 3
+ = 3$6 is that $ is not a legal token in mathematics (at least
as far as I know). Similarly,
2
Zz is not legal because there is no element with the
abbreviation Zz.
The second type of syntax error pertains to the structure of a statement, that is, the
way the tokens are arranged. The statement 3
+ = 3$6 is illegal because even though
+ and = are legal tokens, you can’t have one right after the other. Similarly, in a
chemical formula the subscript comes after the element name, not before.
Exercise 1.1
Write a well-structured English sentence with invalid tokens in it. Then write another
sentence with all valid tokens but with invalid structure.
When you read a sentence in English or a statement in a formal language, you have
to figure out what the structure of the sentence is (although in a natural language
you do this subconsciously). This process is called parsing.


6
The Way of the Program
For example, when you hear the sentence “The penny dropped,” you understand that
“the penny” is the subject and “dropped” is the predicate. Once you have parsed a
sentence, you can figure out what it means, or the semantics of the sentence. Assum-
ing that you know what a penny is and what it means to drop, you will understand
the general implication of this sentence.
Although formal and natural languages have many features in common – tokens,
structure, syntax, and semantics – there are some differences:
ambiguity: Natural languages are full of ambiguity, which people deal with by
using contextual clues and other information. Formal languages are designed
to be nearly or completely unambiguous, which means that any statement has
exactly one meaning, regardless of context.
redundancy: In order to make up for ambiguity and reduce misunderstandings,
natural languages employ lots of redundancy. As a result, they are often
verbose. Formal languages are less redundant and more concise.
literalness: Natural languages are full of idiom and metaphor. If I say, “The
penny dropped,” there is probably no penny and nothing dropping.

Formal
languages mean exactly what they say.
People who grow up speaking a natural language – everyone – often have a hard
time adjusting to formal languages. In some ways, the difference between formal
and natural language is like the difference between poetry and prose, but more so:
Poetry: Words are used for their sounds as well as for their meaning, and the
whole poem together creates an effect or emotional response. Ambiguity is
not only common but often deliberate.
Prose: The literal meaning of words is more important, and the structure con-
tributes more meaning. Prose is more amenable to analysis than poetry but
still often ambiguous.
Programs: The meaning of a computer program is unambiguous and literal, and
can be understood entirely by analysis of the tokens and structure.
Here are some suggestions for reading programs (and other formal languages). First,
remember that formal languages are much more dense than natural languages, so
it takes longer to read them. Also, the structure is very important, so it is usually
not a good idea to read from top to bottom, left to right. Instead, learn to parse the
program in your head, identifying the tokens and interpreting the structure. Finally,
the details matter. Small errors in spelling and punctuation, which you can get away
with in natural languages, can make a big difference in a formal language.
1.5
THE FIRST PROGRAM
Traditionally, the first program you write in a new language is called "Hello,
World!"
because all it does is display the words, "Hello, World!" In Python, it

This idiom means that someone realized something after a period of confusion.


1.6 Debugging
7
looks like this:
print 'Hello, World!'
This is an example of a print statement,

which doesn’t actually print anything on
paper. It displays a value on the screen. In this case, the result is the words
Hello, World!
The quotation marks in the program mark the beginning and end of the text to be
displayed; they don’t appear in the result.
Some people judge the quality of a programming language by the simplicity of the
"Hello, World!"
program. By this standard, Python does about as well as possible.
1.6
DEBUGGING
It is a good idea to read this book in front of a computer so you can try out the
examples as you go. You can run most of the examples in interactive mode, but if
you put the code into a script, it is easier to try out variations.
Whenever you are experimenting with a new feature, you should try to make mis-
takes. For example, in the "Hello, World!" program, what happens if you leave
out one of the quotation marks? What if you leave out both? What if you spell print
wrong?
This kind of experiment helps you remember what you read; it also helps with debug-
ging, because you get to know what the error messages mean. It is better to make
mistakes now and on purpose than later and accidentally.
Programming, especially debugging, sometimes brings out strong emotions. If you
are struggling with a difficult bug, you might feel angry, despondent, or embarrassed.
There is evidence that people naturally respond to computers as if they were people.
††
When they work well, we think of them as teammates, and when they are obstinate
or rude, we respond to them the same way we respond to rude, obstinate people.
Preparing for these reactions might help you deal with them. One approach is to think
of the computer as an employee with certain strengths, like speed and precision, and
particular weaknesses, like lack of empathy and inability to grasp the big picture.

In Python 3.0, print is a function, not a statement, so the syntax is print(
'
Hello, World!
'
). We
will get to functions soon!
††
See Reeves and Nass, The Media Equation: How People Treat Computers, Television, and New Media
Like Real People and Places.


8
The Way of the Program
Your job is to be a good manager: find ways to take advantage of the strengths
and mitigate the weaknesses. And find ways to use your emotions to engage with the
problem, without letting your reactions interfere with your ability to work effectively.
Learning to debug can be frustrating, but it is a valuable skill that is useful for many
activities beyond programming. At the end of each chapter there is a debugging
section, like this one, with my thoughts about debugging. I hope they help!

tải về 1.38 Mb.

Chia sẻ với bạn bè của bạn:
1   2   3   4   5   6   7   8   9   10   ...   83




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương