Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 REVIEW: Vocabulary of Languages English languageProgramming language Verb (action) go, eat, buy, give Function +, string-append, under-21?, distance Proper noun (specific thing) Adelphi, Adam, Rover Literal 3, “hello there”, true Pronoun (thing in context) him, her, it Variable x, greeting, age Improper noun (kind of thing) college, professor, dog, picture Data type number, string, image, boolean Noun phrase (specific thing, not by name) Adam’s mother Expression (+ 3 (* 4 5)), (cube 7)

3 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.

4 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!

5 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.

6 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.)

7 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))

8 I. Scheme Vocabulary

9 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)

10 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)

11 II. Scheme Grammar

12 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.)

13 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).

14 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

15 These are not expressions. Why not? (f define) (cond x) ()

16 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) ()

17 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 ()

18 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

19 III. Some last words

20 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.

21 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.

22 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.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google