מבוא מורחב למדעי המחשב בשפת scheme
DESCRIPTION
מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 1. Outline. Administration Dr. Scheme Functional vs. Imperative Programming Compiler vs. Interpreter Evaluation Rule Scope. תרגולים ועזרים. ספר הקורס (ניתן לקריאה ברשת, קישור מאתר הקורס ( אתר הקורס: www.cs.tau.ac.il/~scheme מתרגלים מעבדה - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/1.jpg)
מבוא מורחב למדעי המחשב Schemeבשפת
1תרגול
![Page 2: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/2.jpg)
Outline
• Administration
• Dr. Scheme
• Functional vs. Imperative Programming
• Compiler vs. Interpreter
• Evaluation Rule
• Scope
2
![Page 3: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/3.jpg)
3
תרגולים ועזרים
ספר הקורס )ניתן לקריאה ברשת, קישור •)מאתר הקורס
www.cs.tau.ac.il/~schemeאתר הקורס: •
מתרגלים•
מעבדה•
פורום )קישור מאתר הקורס(•
![Page 5: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/5.jpg)
5
תרגיליםתרגילים שבועיים שהגשתם חובה.•הכנת התרגילים היא אישית.• שלהן )רלוונטי דוגמאות הרצהתוכניות יוגשו יחד עם •
החל מהתרגיל השני(. מהתרגילים בהצלחה.80%נדרש: לפחות •[email protected]הגשה בדוא"ל: •לכתוב שם, ת.ז., מספר קבוצה, לצרף הצהרה חתומה •
.ולשמור את אישור ההגשהערעור לבודק בכתב – לתיבת הדואר האישית.•.פרטים באתר הקורס•
![Page 6: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/6.jpg)
6
Dr. Scheme התקנת
(:209התקנת המשערך )גירסא •התקנה בבית, דרך אתר הקורס, או
http://www.drscheme.org
:Linuxמותקן במעבדה בבניין שרייבר במערכת הפעלה drscheme-209&
בהפעלה הראשונה )בלבד(Language->Choose Language>-
Graphical )under PLT tab((Run)לאחר השינוי יש ללחוץ על כפתור ה-
![Page 7: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/7.jpg)
7
![Page 8: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/8.jpg)
8
חלון ההגדרות
חלון האינטרקציות
![Page 9: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/9.jpg)
9
בדיקת תחביר
![Page 10: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/10.jpg)
10
שמירת קבצים והדפסתם
![Page 11: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/11.jpg)
11
עבודה בחלון האינטרקציות
![Page 12: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/12.jpg)
12
עבודה בחלון ההגדרות
Click on “Run”
![Page 13: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/13.jpg)
Machine vs. High-Level Language
13
Assembly Code
BinaryCode
Meaning High-Level Language Code
ADD 5 7 15 35 5 7 15
Op. code of ADD command is 35
Adds contents of the memory cell number)address( 5 to the content of cell #7 and put the results into the cell #15
X:=Y+Z
)+ Y Z(
JEQ 5 8 9 48 5 8 9
Op. code of JEQ command is 48
Examines the content of the memory cell #5 . If it’s equal to zero it executes command located in the memory cell #8 , otherwise executes command located in the memory cell #9
If)a==0( then …..else ……end
)IF a exp1 exp2(
A Compiler is a software program that translates)compiles( programs written in high-level language into machine)binary( code
![Page 14: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/14.jpg)
Compiler vs. InterpreterCompiler Interpreter
Translate the whole program into a binary code understandable by hardware
Evaluate commands )or expressions( one by one.
Analyze code only once Analyze every expression each time it is executed
Recompile whole program for any change
Only changed expression/command is reevaluated
14
Dr. Scheme is an interpreter
![Page 15: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/15.jpg)
Functional vs. Imperative Programming
Functional Imperative
Basic paradigm
Evaluation of expressions without side effects )changes in the memory state(
Result of one expression can be used as an argument to another
Execution of commands that change an environment )memory state(
One command change )prepare( environment for another
Example Compute: f)n(=“n* f)n-1( n>1 or 1 if n=1”on n=5;
s:=1; k:=1Repeat 5 times: s:=s*k; k:=k+1;
Effect Evaluates function n! on n=5. No changes in the environment.
Change state of the variable s so it contains 5!. Also change state of the variable k.
15Scheme is a functional programming language )not purely functional(
![Page 16: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/16.jpg)
Why functional programming?
• Program Optimization– Memorize value of expression instead of repeated
evaluation– Do not evaluate an expression if its value is not
used– No dependency => expressions can be evaluated
in different order or in parallel
• Languages– Lisp, Scheme, Haskell, Erlang
16
![Page 17: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/17.jpg)
Syntax vs. Semantics
Syntax Semantics
Which expression is a legal expression?
How to evaluate/execute a legal expression?
A list is a legal expression in Scheme language
Scheme uses the “evaluation rule” to compute value of an expression
17
)# 5 7( -is syntactically correct but has no meaning
)f 5 7( –meaning)value( depends on the meaning of the symbol f
![Page 18: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/18.jpg)
Postfix, Infix , Prefix notation
18
Name Description Example
Infix Operator is in-between operands
)5+7(*8
Postfix Operator is after the operands
5 7 + 8 *
Prefix Operator is before the operands
* + 5 7 8
)* )+ 5 7( 8( : Scheme uses the prefix notation for arithmetic expressions
![Page 19: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/19.jpg)
Reminder: The Scheme Language
Element Example Semantics
Simple Expression
<,+,586 Element’s value
Means of Combination
)+ 1 3 )* 5 7(( Value of an expression
Means of abstraction
)define x 5( Associate name with a value
Means of abstraction
)lambda)x( )* x x(( Create compound procedure )function(
19
![Page 20: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/20.jpg)
Primitive vs. Compound Procedures
• Primitive procedures are part of the Scheme language )not defined using lambda expressions(– Arithmetic)+,*(, IF, DEFINE
• Compound procedures are created by programmer using the lambda expressions– )lambda)x( )+ x 2((
20
![Page 21: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/21.jpg)
Some Scheme ExpressionsExample Description
)+ )- 9 7( 3( Arithmetic expression
)> 5 2()AND #t )> 10 5(()OR #f )>10 20((
Boolean expressions
)define x )+ 5 2(( Name definition
)define g )lambda)a( )+a 1((( Function )compound procedure( definition
)define )g a( )+ a 1((
)define )g b c( )+ b c((
Function definition using syntactic sugar
)g x())lambda)d( )+ d 1(( 2(
Function call )application of compound procedure(
21
![Page 22: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/22.jpg)
Reminder: Syntactic Sugar
• Shortcuts in the syntax, do not add power to the language
– Can write )define )f x( )* x x( (
– Instead of )define f )lambda)x( )* x x(( (
22
![Page 23: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/23.jpg)
23
Example in DrScheme
![Page 24: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/24.jpg)
24
מספרים רציונליים ומרוכבים
![Page 25: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/25.jpg)
25
מספרים גדולים
•Scheme תומך ב"מספרים גדולים", כאלה שגדולים מהמספרים -264, > 232-1המקסימליים שארכיטקטורת מחשב תומכת בהם ) >
1)
![Page 26: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/26.jpg)
Reminder: Environment table
• )define x 5( associates name x to value of 5 in an environment table
• )define f )lambda)x()* x x((( associates name f to the function lambda)x()* x x( in an environment table
26
Name Value
x 5
f lambda)x()* x x(
Environment Table>)+ x 5(10>)f 5(25
![Page 27: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/27.jpg)
Reminder: How to evaluate an expression?
• An expression is usually built out of sub-expressions– )expr_0 expr_1 … exp_n(
• The first sub-expression evaluates to a primitive or compound procedure– )f )+ 2 3( )g 2((
• Need well-defined rule to evaluate any expression
27
![Page 28: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/28.jpg)
Remainder: Evaluation rule1. Reduce step:
Evaluate all sub-expressions )in any order(
If the procedure to apply is a primitive, just do it.
)+ )* 5 2( )* 5 4((
2. Expand step)substitution model( If the procedure to apply is a compound procedure:
Substitute by the body of the procedure while replacing each formal parameter with the corresponding actual arguments
)define )f x( )* x x((
)f 5(-> )* 5 5(
28
What is the order of the application of step 1 and step 2?
)f )* 5 2((
![Page 29: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/29.jpg)
Normal vs. Applicative order
Applicative Normal
Step 0 )f )+ 2 3( ( )f )+ 2 3( (
Step 1 Reduce: )f 5( Expand: )+ )+ 2 3( 1(
Step 2 Expand: )+ 5 1( Reduce: )+ 5 1(
Step 3 6 6
29
)define )f x( )+ x 1((
• Usually same value if expressions have no side effects• Consider ))lambda)x y( )+ x 2(( 1 )/ 1 0((
![Page 30: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/30.jpg)
Why Applicative order?
• Think of the optimization)define )f x( )* x x((
)define )g x( )sqrt x((
Normal order evaluates )sqrt 2( twice
)f )sqrt 2(( )* )sqrt 2( )sqrt 2((
• What would happen if sqrt produced a side effect )e.g. prints on a display(?
30
![Page 31: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/31.jpg)
31
What is the Scheme order?(define (square x) (* x x))(define (sum-of-squares x y) (+ (square x) (square y)))(define (foo a) (sum-of-squares (+ a 1) (* a 2)))(foo 5)
With DrScheme stepper*:
*works for Beginning Student Language level only
![Page 32: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/32.jpg)
Reminder: Special Forms
Evaluation not according to standard rule or/and may have side effects 32
Form Effect Example
)define name expr( Changes environment table )define x )+ 2 1(()define )f x( )+ x 1((
)if a expr1 expr2( Only one out of expr1 and expr2 is evaluated depending on the value of a
)if )= x 0( )myprint 1( )myprint 2((
)and exp1 exp2( If exp1 is false ,exp2 is not evaluated
)and )> 0 a( )> 2 )/ 1 a((
)or exp1 exp2 exp3( If exp1 is true , exp2 is not evaluated, if exp2 is true, exp3 will not be evaluated
)or )= 0 a( )> 1 )/ 2 a((
![Page 33: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/33.jpg)
33
דוגמא – ערך מוחלט
(define (abs x)
(if (< x 0)
(- x)
x))
![Page 34: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/34.jpg)
34
עוד דוגמא
(define (foo a b)
((if (> b 0) + -) a b))
(foo 2 6)
(foo 5 -4)
![Page 35: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/35.jpg)
35
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. Do not substitute for occurrences that are bound by an internal definition.
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 36: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/36.jpg)
Scoping• We have seen two methods to introduce names into a program:
– Define form introduces a name associated with a procedure or an expression:
)define x 5(
)define )f x( )* x x((
– Lambda expression introduces names for formal parameters)define )g a b( )+ a b((
))lambda)a b( )+ a b(( 2 3(
• Need rules to interpret names.
)define x 5(
))lambda)x()+ x 1(( x(36
![Page 37: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/37.jpg)
37
Bounded variables
• A procedure definition binds its formal parameters )their names(
• These names are called bounded variables. • Other variables are free variables
)define y 7(
)define )foo x( )+ x y((
x is bounded to foo, while y is free
![Page 38: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/38.jpg)
38
Scope
• The set of expressions for which binding defines a name is called a scope of this name
• A bound variable has its binding function’s body as a scope, and is unknown outside its scope
• Free variables are known anywhere in the program
• For variables with the same name and overlapping scopes, the more internal binding overrides the other
![Page 39: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/39.jpg)
Example)define y 7(
)define z 5(
)define )foo x z( )+ x z y((
• Foo binds x and z. x and z are bounded to foo
• Scope of x and z is the body of foo
• y is known everywhere, z=5 is only outside foo
• The meaning of foo will not change if we’ll change the names of its formal parameters.
39
![Page 40: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/40.jpg)
Internal Definitions
• Can define a name inside the lambda expression )local to this procedure/lambda expression(
• It’s scope is limited to the rest of the body of the procedure
• )define )g x(
)define a 5(
)* x a((
• Note: Body of a procedure)lambda expression( can consist of multiple sub-expressions.
– The value of the last one is defined to be the value of the procedure
40
![Page 41: מבוא מורחב למדעי המחשב בשפת Scheme](https://reader035.vdocuments.mx/reader035/viewer/2022081502/56815987550346895dc6c811/html5/thumbnails/41.jpg)
41
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. Do not substitute for occurrences that are bound by an internal definition.
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