61
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements of Programming Prof. Dr. Max Mühlhäuser Dr. Guido Rößling

Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Embed Size (px)

Citation preview

Page 1: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Telecooperation/RBG

Technische Universität Darmstadt

Copyrighted material; for TUD student use only

Introduction to Computer Science ITopic 1: Basic Elements of Programming

Prof. Dr. Max MühlhäuserDr. Guido Rößling

Page 2: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

What is Programming?

Let us take a look at what some godfathers of programming have to say:

2

„To program is to understand“Kristen Nygaard

„Programming is a Good Medium for Expressing Poorly Understood and Sloppily Formulated Ideas“

Marvin Minsky, Gerald J. Sussman

Page 3: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

The Elements of Programming

• A powerful programming language (PL) is more than just a means for instructing a computer to perform tasks

• It also serves as a framework within which we organize our ideas about a problem domain

3

When we describe a language, we should pay attention to the means that it provides for combining simple ideas to more complex ones.

Page 4: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

The Elements of Programming

Every powerful language has three mechanisms to structure ideas about processes:

– Primitive expressions• Represent the simplest entities of the language

– Means of combination• Compound elements are built from simpler ones

– Means of abstraction• Compound elements can be named and manipulated as units

4

Page 5: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Keys to Engineering Design

• Primitives– Resistors, capacitors, inductors, voltage sources, …

• Means of combination– Rules for how to wire together in a circuit– Standard interfaces (e.g. voltages, currents) between

elements

• Means of abstraction– “Black box” abstraction – think about sub-circuit as a

unit: e.g. amplifier, modulator, receiver, transmitter, …

5

Page 6: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Language Elements - Primitives• Numbers

– Examples: 23, -36– Numbers are self-evaluating: the values of the digits

are the numbers they denote23 → 23-36 → -36

• Boolean values– true and false – Also self evaluating

• Names for built-in procedures– Examples: +, *, /, -, =, …– What is the value of such an expression?– The value of + is a procedure

• We will later refer to these kinds of values as “first-class procedures”

– Evaluating by looking up the value associated with the name

6

Page 7: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Compound Elements

7

prefix notation

(+ 2 3)left parenthesis

Operator Operands

rightparenthesis

The value of a compound element is determined by executing the procedure (denoted by the operator) with the values of the operands.

Page 8: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

represents the application of the procedure to the numbers

Compound Elements • Compound Elements:

– A sequence of expressions enclosed in parentheses– the expressions are primitives or compounds

themselves• Example:

– Expressions representing numbers may be combined with expressions representing a primitive procedure (+ or *) to form a compound expression

8

(+ 2 3)left parenthesis

Operator Operands

rightparenthesis

Page 9: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Compound Elements

• Can use nested combinations– just apply rules recursively

9

• A combination always denotes a procedure application parentheses cannot be inserted or omitted without changing the meaning of the expression

(+ 4 (* 2 3)) = (4 + (2 * 3)) = 10

(* (+ 3 4) (- 8 2)) = ((3 + 4) * (8 - 2)) = 42

Page 10: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Abstractions

10

• Create a complex thing by combining more primitive things,

• name it, • treat it like a primitive.

(define score (+ 23 7))(define PI 3.14)

• Simple mean of abstraction: define

Page 11: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Naming Compound Elements

• Special form: A bracketed expression, starting with one of the few keywords of scheme

• Example: define – using define we can pair a name with a value

example: (define score (+ 23 7))– The define special form does not evaluate the

second expression (in the example: score)– Rather, it pairs that name with the value of the third

expression in an environment

• The return value of a special form is unspecified

11

Page 12: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Naming and the Environment

• An important aspect of a programming language is the means it provides to refer to computational objects using names. – A name identifies a variable whose value is the

object

• Environment: the interpreter maintains some sort of memory to keep track of the name-object pairs.– Associating values with symbols– Retrieve them later

12

Page 13: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Naming and the Environment

• To get the value of a name, just look it up in environment– Example: the evaluation of score is 30

(define score (+ 27 3))(define total (+ 30 15))(* 100 (/ score total))

– Note: we already did this implicty (looking up a name in an environment) for +, *, …

13

+

*

/

score

total

30

25

Page 14: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Evaluation Rules

1. Self evaluating return the value• The values of digits are the numbers that they name

2. Built-in operator return the machine instruction sequence that carry out the corresponding operations.

3. Name return the value that is associated with that name in the environment.

4. Special form do something special.5. Combination

I. Evaluate the sub expressions (arbitrary order)II. Apply the procedure that is the value of the leftmost sub

expression (the operator) to the arguments that are the values of the other sub expressions (the operands).

Example of a combination: (+ 4 (* 2 3))

14

Page 15: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Evaluating Combinations

• The evaluation rule is recursive as one of its steps, the rule needs to invoke itself– Every element has to be evaluated before the

whole evaluation can be done

• Evaluating the following combination requires that the evaluation rule be applied to 4 different combinations.

15

( * (+ 2 (* 4 6) ) (+ 3 5 7) )

Page 16: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Read-Eval-Print-Loop• define-rule:

– Only evaluate the second operand– The name of the first operand is bound to the

calculated value– The overall value of the expression is undefined

16

(define PI 3.14)

eval

defi

ne-ru

le undefined

pri

nt

Differences betweenScheme versions

Name Value

PI 3.14

”PI --> 3.14"Visible world

Execution world

Page 17: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Read-Eval-Print-Loop

17

23 23

23

calculate

self-rule

pri

nt

Expression

Value

printed representation of the value

PI 3.14

3.14eval

nam

e-rule

pri

nt

Value

Naming-rule: Look-up the value in the currentenvironment using the name

Visible World

Execution World

Expression

Page 18: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Capturing common patterns

• Here are some common patterns (* 3.14 (* 5 5))(* 3.14 (* 23.2 23.2))(* 3.14 (* x x))

• How do we generalize – (e.g. the last expression)?– i.e. how to express the idea of “circle area computation”?

18

They are instances of a circle area computation

Page 19: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Creating new procedures

• We use procedures to capture ways of doing things– sometimes we also use the name function– similar to a function in mathematics

• The define special form is used to create new procedures

19

Name Parameter

(define (area-of-disk r) (* 3.14 (* r r)))

Body of procedure

Page 20: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Creating new Procedures

• As soon as a procedure has been defined, we can use it as if it were a primitive procedure (such as +, * etc.)

– Example - area of a circle:(area-of-disk 5)

= 78.5

– Example - area of a ring:(- (area-of-disk 5) (area-of-disk 3)) = (78.5 - 28.26) = 50.24

20

= -

Page 21: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Creating new procedures

• Existing procedures can be combined to new, more powerful procedures – Example: calculate area of a ring

– Example: Using the new procedure

– (area-of-ring 5 3) = (- (area-of-disk 5) (area-of-disk 3))= (- (* 3.14 (* 5 5)) (* 3.14 (* 3 3)))= … = 50.24

21

(define (area-of-ring outer inner) (- (area-of-disk outer) (area-of-disk inner)))

Page 22: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Informal specifications

• Typical program specification– Usually not in mathematical terms that can be directly

transformed into programs – Often rather informal problem specifications

• May contain irrelevant or ambiguous information

• Example:

22

“Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work.” problem analysis

(define (wage hours) (* 12 hours))

Page 23: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Errors

• Your programs will contain errors– This is normal– Don’t get confused or frustrated by your errors

• Possible errors:– Wrong number of brackets, i.e. (* 3 (5)– The operator of a procedure call is not a procedure

(10)(10 + 20)

– other typical runtime errors:(+ 3 true)(/ 3 0)

• Try out what is happening in erroneous programs and try to understand the error message!

23

Page 24: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Programs

• The design of programs is not trivial• The following design recipe helps you in writing

your first program:– step-by-step prescription of what you should do – Later we will refine this recipe

24

Any program development requires at least the following four activities:

1. Understanding the program's purpose2. Thinking about program examples3. Implementing the program body4. Testing

Page 25: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Programs

1. Understanding the program's purpose– calculate the area of a ring:

– calculate the area of the ring that has an outer radius ‘outer’ and an inner radius ‘inner’

– It can be calculated using the radius of the circle with radius ‘outer’ and subtracting the area of the circle with radius ‘inner’

– …

25

“If you can't write it down in English, you can't code it.“Peter Halpern

Page 26: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Programs

1. Understanding the program's purpose– Giving the program a meaningful name – Definition of a contract

• What kind of information is consumed and produced?– Adding the program header– Formulate a short purpose statement for the program,

that is a brief comment of what the program is to compute

26

;; area-of-ring :: number number -> number;;;; to compute the area of a ring,;; whose hole has a radius of “inner”(define (area-of-ring outer inner) … )

Page 27: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Programs

2. Program Examples – Help to characterize the input and output– Examples help us to understand the computational

process of a program and to discover logical errors – It is easier to understand something difficult with an

exampleFor our example:

27

;; area-of-ring :: number number -> number;; to compute the area of a ring,;; whose hole has a radius of “inner”

;; Example: (area-of-ring 5 2) is 65.94 (define (area-of-ring outer inner) … )

Page 28: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Programs

3. Implement the program body– Replace the “...” in our header with an expression– If the input-output relationship is given as a

mathematical formula, we just translate– In case of an informally stated formula, we have to

understand the computational task • the examples of step 2 can help us

28

;; area-of-ring :: number number -> number;; to compute the area of a ring,;; whose hole has a radius of “inner”;; Example: (area-of-ring 5 2) is 65.94

(define (area-of-ring outer inner) (- (area-of-disk outer) (area-of-disk inner)))

Page 29: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Programs

29

4. Testingi.e. with the DrScheme Testcases(Special -> Insert Testcase)• to discover mistakes• in particular for non local

errors

Page 30: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

30

“Testing can show the presence of bugs, but not their absence.”

Edsger W. Dijkstra

“Beware of bugs in the above code; I have only proved it correct, not tried it“

Donald E. Knuth

Page 31: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Auxiliary Functions

• When should we use auxiliary functions?

• Example:

31

The owner of a performance theater wants you to design a program that computes the relationship between profit and ticket price

• At a price of 5€ per ticket, 120 people attend a performance. • Decreasing the price by 0.10€ increases attendance by 15

people. • Each performance costs the owner 180€.• Each attendee costs 0.04€.

Page 32: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Auxiliary Functions: Bad Design

32

;; How NOT to design a program (define (profit price) (- (* (+ 120

(* (/ 15 .10) (- 5.00 price)))price)

(+ 180 (* .04 (+ 120 (* (/ 15 .10)

(- 5.00 price)))))))

Page 33: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Auxiliary Functions: Good Design

33

;; How to design a program (define (profit ticket-price) (- (revenue ticket-price) (cost ticket-price)))

(define (revenue ticket-price) (* (attendees ticket-price) ticket-price))

(define (cost ticket-price) (+ 180 (* 0.04 (attendees ticket-price))))

(define (attendees ticket-price) (+ 120 (* 15 (/ (- 5.00 ticket-price) 0.10))))

Page 34: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Guideline on Auxiliary Functions

Formulate auxiliary function definitions for every

dependency between • quantities mentioned in the problem

statement or • quantities discovered with example

calculations.

34

Page 35: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Procedures as Black-Box-Abstractions

• Abstraction helps hiding complexity

• Details that are irrelevant for understanding from a special point of view are ignored.

35

Page 36: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Procedures as Black-Box abstractions

36

• Will consider several fundamental kinds of abstraction in this course.

• Procedural abstraction is one:– area-of-ring computes the area of a ring– user doesn't have to think about the internals

Input Output

We know what it does, but not how.

black-box-abstraction

Page 37: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Procedures as Black-Box abstractions

• A computing problem is often broken down into natural, smaller sub-problems.

– Example: • area of a ring 2* Calculating the area of a circle

– Procedures are written for each of these sub problems.• area-of-disk,… primitive procedures …

37

Page 38: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Procedures as Black-Box abstractions

• The procedure attendees can be seen as black box.• We know that it calculates the number of attendees.• But we do not want to know how it works.• These details can be ignored.

• attendees is a procedural abstraction for revenue/cost.

38

profit

revenue cost

attendees

Page 39: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Procedures as Black-Box abstractions• A user defined procedure is called by a name, as

are primitive procedures • How a procedure works remains hidden.

39

At this level of abstraction, any procedure that calculates squares is as good as any other.

(define (square x) (* x x))

(define (square x) (* (* x 10) (/ x 10)))

area-of-ring

area-of-circle

square

Page 40: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

constant definition

Guideline on Variable Definitions:

– Better to read– Easier to maintain

• changes must be made at one point only– In our example:

(define PI 3.14)– We only need one change for a better approximation

(define PI 3.14159)

40

Give names to frequently used constants and use the names instead of the constants in programs

Page 41: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Conditional ExpressionsTwo modes:

1) (if <test> <then-expr> <else-expr>) not optional in

Scheme

Example:

(define (absolute x) (if (< x 0) (- x) x))

41

Page 42: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Conditional ExpressionsTwo modes:

2) (cond [<test1> <expr1>] [<test2> <expr2>] . . . [else <last-expr>]) optional

Example: (define (absolute x) (cond [(> x 0) x] [(= x 0) 0] [else (- x)]))

42

Page 43: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Boolean functions

(and <expr_1> <expr_2> . . . <expr_N>)– <expr_i> (i = 1..N) evaluated in order of

appearance;– returns false if any expression is evaluated to false,else the return is true (shortcut).

– If one of the expressions returns neither true or false, then an error will occur.

– Some expressions will not be evaluated due to the shortcut-Rule

44

(and (= 4 4) (< 5 3)) false(and true (+ 3 5)) Error: and: question result is not true or false: 8(and false (+ 3 5)) Shortcut-Rule: false

Page 44: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Boolean functions

(or <expr_1> <expr_2> . . . <expr_N>)– <expr_i> (i = 1..N) evaluated in order of occurrence;– returns true after the first value is evaluated to true;– returns false if all expressions are false– An error occurs if a value evaluates to neither true or false

45

(or (= 4 4) (< 5 3)) true(or true (+ 3 5)) Shortcut-Rule: true (or false (+ 3 5)) Error: or: question result is not true or false: 8

Page 45: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Boolean functions

(boolean=? <expr1> <expr2>)– expr1, expr2 evaluated in order of appearance; – returns true, if expr1 and expr2 both produce true or

both produce false– returns false, if the operands have different Boolean

values– an error occurs, if an operand evaluates to neither true

or false

(not <expr>)– returns true when <expr> evaluates to false– returns false when <expr> evaluates to true

46

Page 46: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Designing Conditional Functions• How does our design process change?

– New Phase: Data analysis• Which different situations exist?

– Examples• choose at least one example per situation

– Implementing the program body• First write down the skeleton of a cond/if expression, then

implement the individual cases– Testing

• tests should cover all situations

47

Program development requires at least the following four activities1. Understanding the program's purpose2. Making up examples3. Implementing the program body4. Testing

Page 47: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Symbols• Up to this point, we know numbers and Booleans

as primitive values • Often we want to store symbolic information

– names, words, directions• A symbol in Scheme is a sequence of characters,

headed by a single quotation mark:– ‘the ‘dog ‘ate ‘a ‘cat! ‘two^3 ‘and%so%on?– Not all characters are allowed (i.e. no space)

• Only one operation on this data type: symbol=?– (symbol=? ‘hello ‘hello) true– (symbol=? ‘hello ‘abc) false– (symbol=? 1 2) error

• Symbols are atomic (like numbers, Booleans)– Symbols cannot be separated

48

Page 48: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Symbols: Example

49

(define (reply s) (cond [(symbol=? s 'GoodMorning) 'Hi] [(symbol=? s 'HowAreYou?) 'Fine] [(symbol=? s 'GoodAfternoon) 'INeedANap] [(symbol=? s 'GoodEvening) 'BoyAmITired] [else 'Error_in_reply:unknown_case] ))

Page 49: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Symbols vs. Strings• Many of you may know the data type String• Symbols are different from Strings

– Symbols: Are used for symbolic names• atomic, • no manipulation, • very efficient comparison• certain restrictions which characters can be represented

– Strings: Are used for text data• Manipulation possible

– (i.e. search, compose etc.) • Comparison is expansive• Any kind of character (string) is possible

– Strings are also available in Scheme• To generate with a double quotation mark; compare with

string=? – For now we will ignore strings

50

Page 50: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Reminder: The Evaluation-Rule

1. self evaluated …2. built-in operator …3. Name …4. special form …5. Combination

I. Evaluate the sub expressions (in any order)II. Apply the procedure that is the value of the leftmost

sub expression (the operator) to the arguments that are the values of the other sub expressions (the operands).

51

Up to now we have only considered built-in procedures.

How to evaluate procedures that are defined by the programmer?

Page 51: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Extended Evaluation-Rule

• Evaluation rule for procedures

– The procedure is a primitive procedure• execute the respective machine instructions.

– The procedure is a compound procedure• Evaluate the procedure body• Substitute each formal parameter with the respective

actual value, that is passed when applying the procedure.

52

(define (f x ) (* x x)) (f 5 )

Page 52: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Substitution Model

• Fictive names (formal parameters/variables): – allow the definition of general procedures that can

be reused in various situations.

• When using the procedures, the actual values need to be associated with the fictive names.

• As known from algebra:

53

f(a, b) = a2 + b2f(a, b) = a2 + b2

Declaring the formal

parameters

Defining the function using formal parameters

f (3, 2)f (3, 2)Using the general function to solve a particular problem

Page 53: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Substitution Model

54

Associating the actual values when executing

Calling environment

a b

a * a

+

b * b... (define b 2)... (f 3 2) ...... (f b 2) ......

... (define b 2)

... (f 3 2) ...

... (f b 2) ...

...

Procedure environment of f

(define (f a b)(+ (* a a) (* b b)))

Page 54: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Substitution Model

55

... (define b 2)

... (f 3 2) ...

... (f b 2) ...

...

... (define b 2)

... (f 3 2) ...

... (f b 2) ...

...

a b

a * a

+

3 2

3 * 3

13

b * b2 * 2

Associating the actual values when executing

Calling environment Procedure environment of f

(define (f a b)(+ (* a a) (* b b)))

Page 55: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Substitution Model

56

... (define b 2)

... (f 3 2) ...

... (f b 2) ...

...

... (define b 2)

... (f 3 2) ...

... (f b 2) ...

...

a b

a * a

+

Procedure environment of f

2 2

2 * 2

8

b * b2 * 2

Associating the actual value when executing

Calling environment

Page 56: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Substitution Model

• The purpose of the substitution model is to help us think about procedure application– It does not provide a description of how the

interpreter really works– Typically, an interpreter does not evaluate

procedure applications by manipulating the text of a procedure to substitute values for the formal parameters.

• This is a simplified model to get started thinking formally about the evaluation process– More detailed models will follow later on– Allows you to “execute” a program on a piece of

paper

57

Page 57: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Details of the Substitution Model:applicative order of evaluation

(define (square x) (* x x))) (define (average x y) (/ (+ x y) 2)))

(average 5 (square 3)) (average 5 (* 3 3)) (average 5 9) first evaluate the operands,

then do the replacement (applicative order)

(/ (+ 5 9) 2) (/ 14 2) If the operator is a simple

procedure, replace it 7 with the result of the operation

58

Page 58: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Details of the Substitution Model:normal order of evaluation

(define (square x) (* x x))(define (average x y) (/ (+ x y) 2))

(average 5 (square 3)) (/ (+ 5 (square 3)) 2) (/ (+ 5 (* 3 3)) 2) (/ (+ 5 9) 2) (/ 14 2) 7

59

normal order

Page 59: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Applicative vs. normal order of evaluation• Applicative: First evaluate the operator and all operands,

then substitute• Normal: Evaluate operator, then substitute the (not

evaluated) operands for the formal arguments of the operator

• Important, non-trivial property: The result does not depend on the order of evaluation (confluence)– However, termination of the evaluation process may depend on

the evaluation order– We will see features (assignment, in-/output) which destroy this

characteristic later– Sometimes both strategies can be very different in the number

of evaluation steps• argument is not required normal order wins• argument is required several times applicative order wins

60

Page 60: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Review: Scheme• Things that constitute a Scheme program:

– self evaluating 23, true, false, – names +, PI, pi– combinations (+ 2 3) (* pi 4)– special forms (define PI 3.14)

• Syntax– combination: (oper-expression other-expressions …)– special form: a special keyword as first sub routine

• Semantics– combinations: evaluate subroutines in any order,

use the operator for the operands substitution for user-defined procedures

– special forms: each form has its own

61

Page 61: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 1: Basic Elements

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T1

Summary

We have learned:– The simplest elements (data/procedures) of Scheme– Combination as a composing instrument of simpler

elements into more complex elements– How to make combinations to use them further as

elements in other combinations– How to define own procedures – process pattern – and

use them as basic elements of combinations_______________________ Scheme ________________________

– Separate problems to smaller exact defined tasks– Procedures as Black-Box abstraction– Semantic of a procedure call as a substitution process

62