Extended Introduction to Computer Science - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Extended Introduction to Computer Science

Description:

Book: Structure and Interpretation of Computer Programs by Abelson & Sussman ... [Heron of Alexandria] 9. X = 2. G = 1. X/G = 2. G = (1 2) = 1.5. X/G = 4/3 ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 38
Provided by: cstelavivu
Category:

less

Transcript and Presenter's Notes

Title: Extended Introduction to Computer Science


1
Extended Introduction to Computer Science
2
Administration
  • ??? ?????
  • ????? ??? ????, ????? ?????
  • ??????? ??? ????, ??? ???
  • ????? ???? ??????
  • Book Structure and Interpretation of Computer
    Programs by Abelson Sussman
  • Web http//www.cs.tau.ac.il/scheme

3
Course Structure
  • Three Elements
  • Lectures (??????)
  • Recitations (???????)
  • Homework (?????? ???)
  • Final Grade Homework MidTerm Exam Final Exam

???? ????? ????? ???? ???? ???? ????? 80
?????????!!
4
This Course
Is NOT a programming course It is an extended
introduction to COMPUTER SCIENCE
5
Computer Science
6
Geometry
Giha Earth
Metra Measure
7
Declarative Knowledge
What is true
8
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

9
(No Transcript)
10
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

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

12
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

13
The Scheme Interpreter
  • The Read/Evaluate/Print Loop
  • Read an expression
  • Compute its value
  • Print the result
  • Repeat the above
  • The Environment

Name Value
14
Designing Programs
  • Controlling Complexity
  • Problem Decomposition
  • Black Box Abstraction
  • Implementation vs. Interface
  • Modularity

15
Language Elements
Syntax Semantics
16
Computing in Scheme
gt 23
23
gt ( 3 17 5)
25
gt ( 3 ( 5 6) 8 2)
23
score
43
gt (define score 23)
17
Computing in Scheme
gt score
23
gt (define total 25)
23
score
25
total
gt ( 100 (/ score total))
92
92
percentage
gt (define percentage ( 100 (/ score total))
gt
18
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)

19
Using Evaluation Rules
gt (define score 23)
gt ( ( 5 6 ) (- score ( 2 3 2 )))
20
Abstraction Compound Procedures
  • How does one describe procedures?
  • (lambda (x) ( x x))

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

22
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
gt ((lambda(x)( x x)) 5)
23
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
24
Using Abstractions
gt (define square (lambda(x)( x x)))
Environment Table
gt (square 3)
square
Proc (x)( x x)
9
gt ( (square 3) (square 4))
25
Yet More Abstractions
gt (define sum-of-two-squares
(lambda(x y)( (square x) (square y))))
gt (sum-of-two-squares 3 4)
25
gt (define f (lambda(a)
(sum-of-two-squares ( a 3) ( a 3))))
Try it outcompute (f 3) on your own
26
Evaluation of An Expression (reminder)
The Substitution model
? reduction in lambda calculus
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
27
Lets not norget The Environment
gt (define x 8)
gt ( x 1)
9
gt (define x 5)
gt ( x 1)
The value of ( x 1) depends on the environment!
6
28
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

29
Booleans
Two distinguished values denoted by the constants
t and f
The type of these values is boolean
gt (lt 2 3)
t
gt (lt 4 3)
f
30
Values and types
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 (gt x 1) is a boolean (or logical)

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

32
More examples
gt (define x 8)
gt (define x ( x 2))
gt x
16
gt (define x y)
reference to undefined identifier y
gt (define -)
gt ( 2 2)
0
33
The IF special form
(if (lt 2 3) 2 3) gt 2 (if (lt 2 3) 2 (/ 1 0))
gt
ERROR
2

34
IF is a special form
  • In a general form, we first evaluate all
    arguments and then apply the function
  • (if ltpredicategt ltconsequentgt ltalternativegt) is
    different
  • ltpredicategt determines whether we evaluate
    ltconsequentgt or ltalternativegt.
  • We evaluate only one of them !

35
Syntactic Sugar for naming procedures

Instead of writing
(define square (lambda (x) ( x x))
We can write
(define (square x) ( x x))

36
Some examples
(define twice
) (twice 2) gt 4 (twice 3) gt 6
(lambda (x) ( 2 x))
Using syntactic sugar (define (twice x) ( 2
x))
(define second
) (second 2 15 3) gt 15 (second 34 -5 16) gt -5
(lambda (x y z) y)
Using syntactic sugar (define (second x y z) y)
37
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
Write a Comment
User Comments (0)
About PowerShow.com