35
1 Extended Introduction to Computer Science

1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

Embed Size (px)

Citation preview

Page 1: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

1

Extended Introduction to Computer Science

Page 2: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

2

Administration

סגל הקורס:•

מרצים: ד"ר דניאל דויטש, איל כהן–מתרגלת:לבנת ג'רבי– ינון פלד בודק:–

• Book: Structure and Interpretation of Computer Programs – by Abelson & Sussman

• Web: http://www.cs.tau.ac.il/~scheme

Page 3: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

3

Course Structure

• Three Elements– Lectures (הרצאות)– Recitations (תרגולים)– Homework (תרגילי בית)

• Final Grade = Homework (30%) + Final Exam (70%)

חובה להגיש ולקבל ציון עובר עבור לפחות

מהתרגילים!!80%

Page 4: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

4

This Course…

Is NOT a programming course

It is an extended introduction to COMPUTER SCIENCE…

Page 5: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

5

Declarative Knowledge

0 and such that theis 2 yxyyx

“What is true”

Page 6: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

6

To find an approximation of x:

• Make a guess G• Improve the guess by averaging G and x/G• Keep improving the guess until it is good enough

Imperative Knowledge

“How to”

An algorithm due to:

[Heron of Alexandria]

Page 7: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

7

X = 2 G = 1

X/G = 2 G = ½ (1+ 2) = 1.5

X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666

X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156

To find an approximation of x:

• Make a guess G• Improve the guess by averaging G and x/G• Keep improving the guess until it is good enough

.2for :Example xx

Page 8: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

8

“How to” knowledge

Process – series of specific, mechanical steps for deducing information, based on simpler data and set of operations

Procedure – particular way of describing the steps a process will evolve through

Need a language for description:

• vocabulary

• rules for connecting elements – syntax

• rules for assigning meaning to constructs – semantics

Page 9: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

9

Designing Programs

• Controlling Complexity – Black Box Abstraction– Conventional Interfaces– Meta-linguistic Abstraction

Page 10: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

10

Scheme

• We will study LISP = LISt Processing– Invented in 1959 by John McCarthy– Scheme is a dialect of LISP – invented by Gerry

Sussman and Guy Steele

Expressions Data or procedures

Syntax Semantics

Page 11: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

11

The Scheme Interpreter

• The Read/Evaluate/Print Loop– Read an expression

– Compute its value

– Print the result

– Repeat the above• The Environment

score 23

total 25

percentage 92

Name Value

Page 12: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

12

Designing Programs

• Controlling Complexity

– Problem Decomposition

– Black Box Abstraction • Implementation vs. Interface

– Modularity

Page 13: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

13

Language Elements

Means of Abstraction

(define score 23) Associates score with 23 in environment table

Syntax Semantics

Means of Combination

(+ 3 17 5) Application of proc to arguments

Result = 25

Primitives 23+*

23

Proc for adding

Proc for multiplying

Page 14: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

14

Computing in Scheme

==> 23

23

==> (+ 3 17 5)

25

==> (+ 3 (* 5 6) 8 2)

43

==> (define score 23)

Name Value

Environment Table

23score

Opening parenthesis

Expression whose value is a procedure

Other expressions

Closing parenthesis

Page 15: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

15

Computing in Scheme

==> score

23

==> (define total 25)

==> (* 100 (/ score total))

92

==> (define percentage (* 100 (/ score total))

Name Value

Environment Table

23score

25total

92percentage

==>

Page 16: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

16

Evaluation of Expressions

To Evaluate a combination: (as opposed to special form)a. Evaluate all of the sub-expressions in some orderb. Apply the procedure that is the value of the leftmost sub-

expression to the arguments (the values of the other sub-expressions)

The value of a numeral: numberThe value of a built-in operator: machine instructions to executeThe value of any name: the associated value in the environment

Page 17: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

17

Using Evaluation Rules

==> (define score 23)

==> (* (+ 5 6 ) (- score (* 2 3 2 )))

Special Form (second sub-expression is not evaluated)

* + 5 6

11

- 23 * 3 22

12

11

121

Page 18: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

18

Abstraction – Compound Procedures

• How does one describe procedures?

• (lambda (x) (* x x))

To process something multiply it by itself

formal parameters

body

Internal representation

• Special form – creates a “procedure object” and returns it as a “value”

Proc (x) (* x x)

Page 19: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

19

Lambda

• The use of the word “lambda” is taken from lambda calculus.

– Introduced in the Discrete Math course.– Useful notation for functions.

Page 20: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

20

Evaluation of An Expression

To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

replaced by the corresponding actual values

==> ((lambda(x)(* x x)) 5)

Proc(x)(* x x) 5

(* 5 5)

25

Page 21: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

21

Evaluation of An Expression

To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

replaced by the corresponding actual values

To Evaluate a combination: (other than special form)a. Evaluate all of the sub-expressions in any orderb. Apply the procedure that is the value of the leftmost sub-

expression to the arguments (the values of the other sub-expressions)

The value of a numeral: numberThe value of a built-in operator: machine instructions to executeThe value of any name: the associated object in the environment

Page 22: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

22

Using Abstractions

==> (square 3)

9

==> (+ (square 3) (square 4))

==> (define square (lambda(x)(* x x)))

(* 3 3) (* 4 4)

9 16+

25

Environment Table

Name Valuesquare Proc (x)(* x x)

Page 23: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

23

Yet More Abstractions

==> (define f (lambda(a) (sum-of-two-squares (+ a 3) (* a 3))))

==> (sum-of-two-squares 3 4)

25

==> (define sum-of-two-squares (lambda(x y)(+ (square x) (square y))))

Try it out…compute (f 3) on your own

Page 24: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

24

Evaluation of An Expression (reminder)

To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

substituted by the corresponding actual values

To Evaluate a combination: (other than special form)a. Evaluate all of the sub-expressions in any orderb. Apply the procedure that is the value of the leftmost sub-

expression to the arguments (the values of the other sub-expressions)

The value of a numeral: numberThe value of a built-in operator: machine instructions to executeThe value of any name: the associated object in the environment

The Substitution

model

reductionin lambda

calculus

Page 25: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

25

Lets not forget The Environment

==> (define x 8)

==> (+ x 1)

9

==> (define x 5)

==> (+ x 1)

6The value of (+ x 1) depends on the environment!

Page 26: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

26

Using the substitution model

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

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

then substitute

(/ (+ 5 9) 2)(/ 14 2) if operator is a primitive procedure, 7 replace by result of operation

Page 27: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

27

Booleans

Two distinguished values denoted by the constants

#t and #f

The type of these values is boolean

==> (< 2 3)

#t

==> (< 4 3)

#f

Page 28: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

28

Values and types

Values have types. For example:

In scheme almost every expression has a value

Examples:

1) The value of 23 is 232) The value of + is a primitive procedure for addition3) The value of (lambda (x) (* x x)) is the compound

procedure proc (x) (* x x)

1) The type of 23 is numeral 2) The type of + is a primitive procedure3) The type of proc (x) (* x x) is a compound procedure4) The type of (> x 1) is a boolean (or logical)

Page 29: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

29

No Value?• In scheme almost every expression has a value

• Why almost?

Example : what is the value of the expression(define x 8)

• In scheme, the value of a define expression is “undefined” . This means “implementation-dependent”

• Dr. Scheme does not return (print) any value for a define expression.

• Other interpreters may act differently.

Page 30: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

30

More examples

==> (define x 8)

Name Value

Environment Table

8x

==> (define x (* x 2))

==> x

16 16

==> (define x y)

reference to undefined identifier: y

==> (define + -)

>-<#+

==> (+ 2 2)

0

Page 31: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

31

The IF special form

ERROR2

(if <predicate> <consequent> <alternative>)If the value of <predicate> is #t,

Evaluate <consequent> and return it

Otherwise

Evaluate <alternative> and return it

(if (< 2 3) 2 3) ==> 2

(if (< 2 3) 2 (/ 1 0)) ==>

Page 32: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

32

IF is a special form

•In a general form, we first evaluate all arguments and then apply the function

•(if <predicate> <consequent> <alternative>) is different:

<predicate> determines whether we evaluate <consequent> or <alternative>.

We evaluate only one of them !

Page 33: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

33

Syntactic Sugar for naming procedures

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

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

Instead of writing:

We can write:

Page 34: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

34

(define second )

(second 2 15 3) ==> 15

(second 34 -5 16) ==> -5

Some examples:

(lambda (x) (* 2 x))

(lambda (x y z) y)

Using “syntactic sugar”:

(define (twice x) (* 2 x))

Using “syntactic sugar”:

(define (second x y z) y)

(define twice )

(twice 2) ==> 4

(twice 3) ==> 6

Page 35: 1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק:

35

Summary• Computer science formalizes the computational process• Programming languages are a way to describe this process

• Syntax• Sematics

• Scheme is a programming language whose syntax is structured around compound expressions

• We model the semantics of the scheme via the substitution model, the mathematical equivalent of lambda calculus