Download Lecture 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Subroutine wikipedia , lookup

Reactive programming wikipedia , lookup

Monad (functional programming) wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

?: wikipedia , lookup

One-pass compiler wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Go (programming language) wikipedia , lookup

Scheme (programming language) wikipedia , lookup

C syntax wikipedia , lookup

Transcript
Extended Introduction to
Computer Science
1
Administration
:‫• סגל הקורס‬
‫ איל כהן‬,‫ ד"ר דניאל דויטש‬:‫– מרצים‬
‫לבנת ג'רבי‬:‫– מתרגלת‬
‫ ינון פלד‬:‫– בודק‬
• Book: Structure and Interpretation of Computer Programs –
by Abelson & Sussman
• Web: http://www.cs.tau.ac.il/~scheme
2
Course Structure
• Three Elements
– Lectures )‫(הרצאות‬
– Recitations )‫(תרגולים‬
– Homework )‫(תרגילי בית‬
• Final Grade = Homework (30%) + Final Exam (70%)
‫חובה להגיש ולקבל ציון‬
80% ‫עובר עבור לפחות‬
!!‫מהתרגילים‬
3
This Course…
Is NOT a programming course
It is an extended introduction to
COMPUTER SCIENCE…
4
Declarative Knowledge
“What is true”
x is the y such that y 2  x and y  0
5
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
An algorithm due to:
[Heron of Alexandria]
6
Example :
x for x  2.
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
X/G = 2
X/G = 4/3
G=1
G = ½ )1+ 2) = 1.5
G = ½ )3/2 + 4/3) = 17/12 = 1.416666
X/G = 24/17 G = ½ )17/12 + 24/17) = 577/408 = 1.4142156
7
“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
8
Designing Programs
• Controlling Complexity
– Black Box Abstraction
– Conventional Interfaces
– Meta-linguistic Abstraction
9
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
Syntax
Data or procedures
Semantics
10
The Scheme Interpreter
• The Read/Evaluate/Print Loop
– Read an expression
– Compute its value
– Print the result
– Repeat the above
• The Environment
Name
score
Value
23
total
25
percentage 92
11
Designing Programs
• Controlling Complexity
– Problem Decomposition
– Black Box Abstraction
• Implementation vs. Interface
– Modularity
12
Language Elements
Primitives
Means of
Combination
Means of
Abstraction
Syntax
23
+
*
(+ 3 17 5)
(define score 23)
Semantics
23
Proc for adding
Proc for multiplying
Application of proc to
arguments
Result = 25
Associates score with
23 in environment
table
13
Computing in Scheme
==> 23
Expression whose
value is a procedure
23
==> (+ 3 17 5)
Closing parenthesis
Environment Table
25
Other expressions
Name
Opening parenthesis
==> (+ 3 (* 5 6) 8 2)
score
Value
23
43
==> (define score 23)
14
Computing in Scheme
==> score
Environment Table
23
Name
==> (define total 25)
score
23
==> (* 100 (/ score total))
total
25
92
percentage 92
Value
==> (define percentage (* 100 (/ score total))
==>
15
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)
a. Evaluate all of the sub-expressions in some order
b. Apply the procedure that is the value of the leftmost subexpression to the arguments (the values of the other subexpressions)
16
Using Evaluation Rules
==> (define score 23)
==> (* (+
*
+
5
6 ) (-
5
6
-
Special Form (second subexpression is not evaluated)
score
23
(*
2
3
2 )))
*
2
3
2
11
12
11
121
17
Abstraction – Compound Procedures
• How does one describe procedures?
formal parameters
• (lambda (x) (* x x))
body
To process something
multiply it by itself
• Special form – creates a “procedure object” and
returns it as a “value”
Proc (x) (* x x)
Internal representation
18
Lambda
• The use of the word “lambda” is taken from
lambda calculus.
– Introduced in the Discrete Math course.
– Useful notation for functions.
19
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
20
Evaluation of An Expression
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 Evaluate a combination: (other than special form)
a. Evaluate all of the sub-expressions in any order
b. Apply the procedure that is the value of the leftmost subexpression to the arguments (the values of the other subexpressions)
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
21
Using Abstractions
==> (define square (lambda(x)(* x x)))
Environment Table
==> (square 3)
9
Name
Value
square
Proc (x)(* x x)
==> (+ (square 3) (square 4))
(* 3 3)
+
9
(* 4 4)
16
25
22
Yet More Abstractions
==> (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
23
Evaluation of An Expression (reminder)
The
 reduction
The value of a numeral: number
in lambda
Substitution
The value of a built-in
operator: machine instructions
to execute
The value of any name:
the associated object inmodel
the environment
calculus
To Evaluate a combination: (other than special form)
a. Evaluate all of the sub-expressions in any order
b. Apply the procedure that is the value of the leftmost subexpression to the arguments (the values of the other subexpressions)
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
24
Lets not forget The Environment
==> (define x 8)
==> (+ x 1)
9
==> (define x 5)
==> (+ x 1)
6
The value of (+ x 1) depends
on the environment!
25
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
26
Booleans
Two distinguished values denoted by the constants
#t and #f
The type of these values is boolean
==> (< 2 3)
#t
==> (< 4 3)
#f
27
Values and types
In scheme almost every expression has a value
Examples:
1) The value of 23 is 23
2) The value of + is a primitive procedure for addition
3) The value of (lambda (x) (* x x)) is the compound
procedure proc (x) (* x x)
Values have types. For example:
1)
2)
3)
4)
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)
28
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.
29
More examples
==> (define x 8)
==> (define x (* x 2))
==> x
16
==> (define x y)
Environment Table
Name
Value
x
16 8
+
#<->
reference to undefined identifier: y
==> (define + -)
==> (+ 2 2)
0
30
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
31
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 !
32
Syntactic Sugar for naming procedures
Instead of writing:
(define square (lambda (x) (* x x))
We can write:
(define (square x) (* x x))
33
Some examples:
(define twice
)
(lambda (x) (* 2 x))
(twice 2) ==> 4
(twice 3) ==> 6
Using “syntactic sugar”:
(define (twice x) (* 2 x))
(define second
(lambda (x y z) y)
)
(second 2 15 3) ==> 15
(second 34 -5 16) ==> -5
Using “syntactic sugar”:
(define (second x y z) y)
34
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
35