Title: Extended Introduction to Computer Science
1Extended Introduction to Computer Science
2Administration
- ??? ?????
- ????? ??? ????, ????? ?????
- ??????? ??? ????, ??? ???
- ????? ???? ??????
- Book Structure and Interpretation of Computer
Programs by Abelson Sussman - Web http//www.cs.tau.ac.il/scheme
3Course Structure
- Three Elements
- Lectures (??????)
- Recitations (???????)
- Homework (?????? ???)
- Final Grade Homework MidTerm Exam Final Exam
???? ????? ????? ???? ???? ???? ????? 80
?????????!!
4This Course
Is NOT a programming course It is an extended
introduction to COMPUTER SCIENCE
5Computer Science
6Geometry
Giha Earth
Metra Measure
7Declarative Knowledge
What is true
8Imperative 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)
10How 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
11Designing Programs
- Controlling Complexity
- Black Box Abstraction
- Conventional Interfaces
- Meta-linguistic Abstraction
12Scheme
- 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
13The Scheme Interpreter
- The Read/Evaluate/Print Loop
- Read an expression
- Compute its value
- Print the result
- Repeat the above
- The Environment
Name Value
14Designing Programs
- Controlling Complexity
- Problem Decomposition
- Black Box Abstraction
- Implementation vs. Interface
- Modularity
15Language Elements
Syntax Semantics
16Computing in Scheme
gt 23
23
gt ( 3 17 5)
25
gt ( 3 ( 5 6) 8 2)
23
score
43
gt (define score 23)
17Computing 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
18Evaluation 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)
19Using Evaluation Rules
gt (define score 23)
gt ( ( 5 6 ) (- score ( 2 3 2 )))
20Abstraction Compound Procedures
- How does one describe procedures?
- (lambda (x) ( x x))
21Lambda
- The use of the word lambda is taken from lambda
calculus. - Introduced in the Discrete Math course.
- Useful notation for functions.
22Evaluation 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)
23Evaluation 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
24Using Abstractions
gt (define square (lambda(x)( x x)))
Environment Table
gt (square 3)
square
Proc (x)( x x)
9
gt ( (square 3) (square 4))
25Yet 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
26Evaluation 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
27Lets 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
28Using 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
29Booleans
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
30Values 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)
31No 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.
32More 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
33The IF special form
(if (lt 2 3) 2 3) gt 2 (if (lt 2 3) 2 (/ 1 0))
gt
ERROR
2
34IF 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))
36Some 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)
37Summary
- 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