close

Вход

Забыли?

вход по аккаунту

код для вставкиСкачать
CSC 160
Computer Programming
for Non-Majors
Lecture #12: Syntax and Semantics
Prof. Adam M. Wittenstein
[email protected]
http://www.adelphi.edu/~wittensa/csc160/
REVIEW: Vocabulary of Languages
English language
Verb (action)
go, eat, buy, give
Proper noun (specific thing)
Adelphi, Adam, Rover
Pronoun (thing in context)
him, her, it
Programming language
Function
+, string-append, under-21?, distance
Literal
3, “hello there”, true
Variable
x, greeting, age
Improper noun (kind of thing) Data type
college, professor, dog, picture
Noun phrase (specific thing,
not by name) Adam’s mother
number, string, image, boolean
Expression
(+ 3 (* 4 5)), (cube 7)
Syntax of the English Language
--Start each sentence with a capital letter.
--End each sentence with a period.
--Include at least one noun in a sentence.
--Include at least one verb in a sentence.
--Etc.
Syntax of the Scheme Language
--Calling a function (function-name argument[s])
--Defining a variable (define VAR-NAME value)
--Defining a function
(define (function-name param-name[s])
…body including param-name[s]…)
--Conditional Expressions
(cond
[question…answer]
[question…answer])
--That’s it!
Similarities of English and Scheme
• They both share the characteristics of
language:
--They have vocabulary and syntax. In
English, syntax is called grammar.
--They both have phrases. In Scheme,
they are called expressions.
--They both have sentences. In Scheme,
they are called functions.
Similarities of English and Scheme
• They both have simple data: numbers,
words, sentences, booleans, images, etc.
• They both have compound data, such as
lists and social security records.
(If you read further in How to Design Programs,
you can see how these are done in Scheme.)
Similarities of English and Scheme
• They both have meaningful sentences.
--English: The sky is blue.
--Scheme: (define (square num) (* num num))
• They both have meaningless sentences.
--English: The brick is a car.
--Scheme: (define (sum x y) (- x y))
I. Scheme Vocabulary
Scheme’s Vocabulary
• Scheme's basic vocabulary consists of five
categories of words:
--constants (sometimes called literals)
--variables
--data types
--expressions
--functions (predefined functions are often
called primitives)
Scheme’s punctuation
• In English, we have periods, commas,
colons, semicolons, etc.
• In Scheme, the punctuation consists of:
--parentheses (one for every function call)
--keywords (define, cond, else)
II. Scheme Grammar
Scheme’s Grammar
• It is well known that English has a more
complicated grammar than most spoken
languages (e.g. Spanish, French, etc.)
• However, Scheme has a far simpler grammar
than most programming languages. In fact, you
have gone through a whole semester using only
4 syntax rules. (Most of us used 4 grammar
rules in English before we finished preschool.)
Syntax Rule #3: Defining Functions
• A function definition is formed by:
--Using (
--Followed by the keyword define
--Followed by another (
--Followed by a non-empty sequence of variables, the
first being the function’s name, and the rest being the
parameter’s name[s].
--Followed by `)
--Followed by an expression
--And closed by a )' that matches the very first one.
• The keyword define distinguishes function definitions
from expressions (such as function calls).
Types of Expressions
•
•
•
•
•
•
Constants:: 5, “blue”, true, etc.
Variables: PI, WIDTH, etc.
Calling primitive functions: (+ 3 4), etc.
Calling user-defined functions: (cube 5), etc.
Conditionals without an else clause
Conditionals with an else clause
These are not expressions.
Why not?
• (f define)
• (cond x)
• ()
These are not expressions.
Why not?
• (f define) Although it partially matches the shape
of a function application but it uses define as if it
were a variable.
• (cond x)
• ()
These are not expressions.
Why not?
• (f define) Although it partially matches the shape
of a function application but it uses define as if it
were a variable.
• (cond x) it contains a variable as the second
item and not a pair of expressions (question and
answer) surrounded by brackets
• ()
These are not expressions.
Why not?
• (f define) Although it partially matches the shape
of a function application but it uses define as if it
were a variable.
• (cond x) it contains a variable as the second
item and not a pair of expressions (question and
answer) surrounded by brackets
• () there must be the same number of pairs of
parentheses as functions, so since there are no
functions, there cannot be any parentheses
III. Some last words
In conclusion…
• Although any language, whether its English, Spanish, or
Scheme has its rules to contend with, believe or not, we
have avoided having to learn the many rules that
ordinary programming languages require. You have only
had to learn FOUR rules the entire semester, compared
with the tens, if not hundreds, of rules required in a first
semester collegiate Java or C++ course.
• The essence of what I tried to convey during the course
is two things:
one, relating one quantity to another quantity, and
two, evaluating a relationship by substituting values for
names.
On computing…
• From elementary school to high school we learn to
compute with one form of data: numbers.
• Computing with software is algebra for all kinds of data,
not just numbers. Nowadays, computer programs
process representations of music, molecules, law cases,
electrical diagrams, architectures of houses, and poems.
In this book we have studied the laws of basic operations
and the laws of operation combination.
• Because the computer is extremely fast and good at
using these laws, it can perform such evaluations for
more data and for larger programs than we can do with
paper and pencil.
On Programming…
• Programs consist of definitions and expressions.
• Large programs consist of hundreds and thousands of
definitions and expressions.
• Programmers design functions, use other programmer's
functions, leave, start on the project.
• Without a strong discipline we cannot hope to produce
software of high quality.
• The key to programming discipline is to understand the
design of programs as a means to describe
computations, which are the manipulate of data through
combinations of operations.
1/--страниц
Пожаловаться на содержимое документа