+ All Categories
Home > Documents > CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M....

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

Date post: 02-Jan-2016
Category:
Upload: antony-wilfred-baldwin
View: 214 times
Download: 0 times
Share this document with a friend
30
CSC 160 CSC 160 Computer Programming Computer Programming for Non-Majors for Non-Majors Chapter 8: Scheme Chapter 8: Scheme Language Review Language Review Prof. Adam M. Wittenstein Prof. Adam M. Wittenstein [email protected] [email protected] http://www.adelphi.edu/~wittensa/csc160/ http://www.adelphi.edu/~wittensa/csc160/
Transcript

CSC 160CSC 160Computer ProgrammingComputer Programming

for Non-Majorsfor Non-Majors

Chapter 8: Scheme Language ReviewChapter 8: Scheme Language Review

Prof. Adam M. WittensteinProf. Adam M. Wittenstein

[email protected]@adelphi.edu

http://www.adelphi.edu/~wittensa/csc160/http://www.adelphi.edu/~wittensa/csc160/

REVIEW: Vocabulary of Languages

English language Programming 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 typenumber, string, image, boolean, posn

Noun phrase (specific thing, not by name) Adam’s mother

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

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

Section 8.1: Scheme Section 8.1: Scheme VocabularyVocabulary

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)

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)

Section 8.2: Scheme Section 8.2: Scheme GrammarGrammar

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

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

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

Lab #6Lab #6

Complete exercises 8.2.3 and 8.2.4 Complete exercises 8.2.3 and 8.2.4 from the textbook on paper. from the textbook on paper.

Exercise 8.2.3 Solution

• (x)

• (+ 1 (not x))

• (+ 1 2 3)

Exercise 8.2.3 Solution

• (x) ILLEGAL, sets of parentheses = 1 and number of functions = 0

• (+ 1 (not x))

• (+ 1 2 3)

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)

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

Exercise 8.2.4 Solution

• (define (f x) 'x)

• (define (f 'x) x)

• (define (f x y) (+ 'y (not x)))

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

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

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

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.


Recommended