Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extended Introduction to Computer Science

Similar presentations


Presentation on theme: "Extended Introduction to Computer Science"— Presentation transcript:

1 Extended Introduction to Computer Science

2 Administration סגל הקורס: מרצים: דניאל דויטש, ניר שביט
מתרגלים: ניר אטיאס, סשה אפראצין , איל כהן בודקת: עמית ליכטנברג Book: Structure and Interpretation of Computer Programs – by Abelson & Sussman Web:

3 Course Structure Three Elements Lectures (הרצאות)
Recitations (תרגולים) Homework (תרגילי בית) Final Grade = Homework + MidTerm Exam + Final Exam חובה להגיש ולקבל ציון עובר עבור לפחות 80% מהתרגילים!!

4 Computer Science

5 Geometry Giha Earth Metra Measure

6 Declarative Knowledge
“What is true” Lets see an example.

7 Imperative Knowledge “How to” 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 [Heron of Alexandria]

8 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 X = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = X/G = 24/17 G = ½ (17/ /17) = 577/408 =

9 “How to” knowledge Scheme
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 Scheme Need a language for description: vocabulary rules for connecting elements – syntax rules for assigning meaning to constructs – semantics

10 Designing Programs Controlling Complexity Black Box Abstraction
Conventional Interfaces Meta-linguistic Abstraction

11 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

12 The Scheme Interpreter
The Read/Evaluate/Print Loop Read an expression Compute its value Print the result Repeat the above The Environment Name Value score 23 total 25 percentage 92

13 Designing Programs Controlling Complexity Problem Decomposition
Black Box Abstraction Implementation vs. Interface Modularity

14 Language Elements Syntax Semantics Primitives 23 + * Proc for adding
Proc for multiplying Means of Combination ( ) Application of proc to arguments Result = 25 Means of Abstraction (define score 23) Associates score with 23 in environment table

15 Computing in Scheme Environment Table score 23
Expression whose value is a procedure ==> 23 Closing parenthesis 23 ==> ( ) Name Value Environment Table Opening parenthesis Other expressions 25 ==> (+ 3 (* 5 6) 8 2) score 23 43 ==> (define score 23)

16 Computing in Scheme Environment Table score 23 total 25 percentage 92
Name Value Environment Table 23 ==> (define total 25) score 23 total 25 ==> (* 100 (/ score total)) 92 percentage 92 ==> (define percentage (* 100 (/ score total)) ==>

17 Evaluation of Expressions
The value of a numeral: number The value of a built-in operator: machine instructions to execute The value of any name: the associated value in the environment To Evaluate a combination: (as opposed to special form) Evaluate all of the sub-expressions in some order Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions)

18 Using Evaluation Rules
==> (define score 23) Special Form (second sub-expression is not evaluated) ==> (* ( ) (- score (* ))) * + 5 6 11 - 23 * 3 2 12 11 121

19 Abstraction – Compound Procedures
How does one describe procedures? (lambda (x) (* x x)) formal parameters something multiply it by itself body To process Internal representation Special form – creates a “procedure object” and returns it as a “value” Proc (x) (* x x)

20 Lambda The use of the word “lambda” is taken from lambda calculus.
Introduced in the Discrete Math course. Useful notation for functions.

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 ==> ((lambda(x)(* x x)) 5) Proc(x)(* x x) 5 (* 5 5) 25

22 Evaluation of An Expression
To Evaluate a combination: (other than special form) Evaluate all of the sub-expressions in any order 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: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment 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

23 Using Abstractions Environment Table Name Value
==> (define square (lambda(x)(* x x))) Environment Table ==> (square 3) Name Value 9 square Proc (x)(* x x) ==> (+ (square 3) (square 4)) (* 3 3) (* 4 4) 9 16 + 25

24 Yet More Abstractions Try it out…compute (f 3) on your own
==> (define sum-of-two-squares (lambda(x y)(+ (square x) (square y)))) ==> (sum-of-two-squares 3 4) 25 ==> (define f (lambda(a) (sum-of-two-squares (+ a 3) (* a 3)))) Try it out…compute (f 3) on your own

25 Evaluation of An Expression (reminder)
 reduction in lambda calculus The Substitution model To Evaluate a combination: (other than special form) Evaluate all of the sub-expressions in any order 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: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment 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

26 Let’s Not Forget The Environment
==> (define x 8) ==> (+ x 1) 9 ==> (define x 5) ==> (+ x 1) The value of (+ x 1) depends on the environment! 6

27 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

28 Booleans Two distinguished values denoted by the constants #t and #f
The type of these values is boolean ==> (< 2 3) #t ==> (< 4 3) #f

29 Values and types Values have types. For example:
In scheme almost every expression has a value Examples: The value of 23 is 23 The value of + is a primitive procedure for addition The value of (lambda (x) (* x x)) is the compound procedure proc (x) (* x x) Values have types. For example: The type of 23 is numeral The type of + is a primitive procedure The type of proc (x) (* x x) is a compound procedure The type of (> x 1) is a boolean (or logical)

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

31 More examples Environment Table ==> (define x 8)
==> (define x (* x 2)) Name Value Environment Table ==> x 8 x 16 16 #<-> + ==> (define x y) reference to undefined identifier: y ==> (define + -) ==> (+ 2 2)

32 The IF special form (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)) ==> ERROR 2

33 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 ! (If (attack Russians) (send bomb) (do nothing)) (If (= I 1) (set I 2) (set I 3)) If + recursion

34 Syntactic Sugar for naming procedures
Instead of writing: (define square (lambda (x) (* x x)) We can write: (define (square x) (* x x))

35 Some examples: Using “syntactic sugar”: (define (twice x) (* 2 x))
(lambda (x) (* 2 x)) Using “syntactic sugar”: (define (twice x) (* 2 x)) (define second ) (second ) ==> 15 (second ) ==> -5 (lambda (x y z) y) Using “syntactic sugar”: (define (second x y z) y)

36 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


Download ppt "Extended Introduction to Computer Science"

Similar presentations


Ads by Google