Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review 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, (make-posn 3 4) Pronoun (thing in context) him, her, it Variable x, greeting, age, a-posn Improper noun (kind of thing) college, professor, dog, picture Data type number, string, image, boolean, posn 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 (in Scheme, symbols), etc. They both have compound data: social security records, shopping, lists, etc. (We only had a brief taste with compound data this semester, in studying Posns.)

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 Section 8.1: Scheme Vocabulary

9 Scheme’s Vocabulary Scheme's basic vocabulary consists of five categories of words: --literals (often called constants) --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, draw, produce, else, cond)

11 Section 8.2: 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 functions definitions from expressions (function calls).

14 Types of Expressions Literals: 5, “blue”, true, etc. Variables: ORIGIN, PI, etc. Calling primitive functions: (+ 3 4), etc. Calling 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 Lab #6 Complete exercises 8.2.3 and 8.2.4 from the textbook on paper. Complete exercises 8.2.3 and 8.2.4 from the textbook on paper.

20 Exercise 8.2.3 Solution (x) (+ 1 (not x)) (+ 1 2 3)

21 Exercise 8.2.3 Solution (x) ILLEGAL, sets of parentheses = 1 and number of functions = 0 (+ 1 (not x)) (+ 1 2 3)

22 Exercise 8.2.3 Solution (x) ILLEGAL, sets of parentheses = 1 and number of functions = 0 (+ 1 (not x)) ILLEGAL, the addition operator requires two numbers, but it is given a number 1 and a boolean (not x). (+ 1 2 3)

23 Exercise 8.2.3 Solution (x) ILLEGAL, sets of parentheses = 1 and number of functions = 0 (+ 1 (not x)) ILLEGAL, the addition operator requires two numbers, but it is given a number 1 and a boolean (not x). (+ 1 2 3) LEGAL, sets of parentheses= 1 and number of functions = 1. Also, follows the contract of +.

24 Exercise 8.2.4 Solution (define (f x) 'x) (define (f 'x) x) (define (f x y) (+ 'y (not x)))

25 Exercise 8.2.4 Solution (define (f x) 'x) LEGAL, it has a header, body, and a right parenthesis at the end. (define (f 'x) x) (define (f x y) (+ 'y (not x)))

26 Exercise 8.2.4 Solution (define (f x) 'x) LEGAL, it has a header, body, and a right parenthesis at the end. (define (f 'x) x) ILLEGAL, a constant like ‘x cannot be in the header (define (f x y) (+ 'y (not x)))

27 Exercise 8.2.4 Solution (define (f x) 'x) LEGAL, it has a header, body, and a right parenthesis at the end. (define (f 'x) x) ILLEGAL, a constant like ‘x cannot be in the header (define (f x y) (+ 'y (not x))) ILLEGAL, parameter names do not have their own set of parentheses

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

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

30 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 Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google