410 likes | 623 Views
מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 1. Outline. Administration Dr. Scheme Functional vs. Imperative Programming Compiler vs. Interpreter Evaluation Rule Scope. תרגולים ועזרים. ספר הקורס (ניתן לקריאה ברשת, קישור מאתר הקורס ( אתר הקורס: www.cs.tau.ac.il/~scheme מתרגלים מעבדה
E N D
Outline • Administration • Dr. Scheme • Functional vs. Imperative Programming • Compiler vs. Interpreter • Evaluation Rule • Scope
תרגולים ועזרים • ספר הקורס (ניתן לקריאה ברשת, קישור מאתר הקורס( • אתר הקורס: www.cs.tau.ac.il/~scheme • מתרגלים • מעבדה • פורום (קישור מאתר הקורס)
תרגילים • תרגילים שבועיים שהגשתם חובה. • הכנת התרגילים היא אישית. • תוכניות יוגשו יחד עם דוגמאות הרצה שלהן (רלוונטי החל מהתרגיל השני). • נדרש: לפחות 80% מהתרגילים בהצלחה. • הגשה בדוא"ל: scheme@post.tau.ac.il • לכתוב שם, ת.ז., מספר קבוצה, לצרף הצהרה חתומה ולשמור את אישור ההגשה. • ערעור לבודק בכתב – לתיבת הדואר האישית. • פרטים באתר הקורס.
Dr. Scheme התקנת • התקנת המשערך (גירסא 209): התקנה בבית, דרך אתר הקורס, או http://www.drscheme.org מותקן במעבדה בבניין שרייבר במערכת הפעלה Linux: drscheme-209 & בהפעלה הראשונה (בלבד) Language->Choose Language-> Graphical (under PLT tab) (לאחר השינוי יש ללחוץ על כפתור ה-Run) !
חלון ההגדרות חלון האינטרקציות
עבודה בחלון ההגדרות Click on “Run”
Machine vs. High-Level Language A Compiler is a software program that translates(compiles) programs written in high-level language into machine(binary) code
Compiler vs. Interpreter Dr. Scheme is an interpreter
Functional vs. Imperative Programming Scheme is a functional programming language (not purely functional)
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
Syntax vs. Semantics (# 5 7) -is syntactically correct but has no meaning (f 5 7) –meaning(value) depends on the meaning of the symbol f
Postfix, Infix , Prefix notation (* (+ 5 7) 8) : Scheme uses the prefix notation for arithmetic expressions
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))
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)) )
מספרים גדולים • Scheme תומך ב"מספרים גדולים", כאלה שגדולים מהמספרים המקסימליים שארכיטקטורת מחשב תומכת בהם ( > 232-1, > 264-1)
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 Environment Table >(+ x 5) 10 >(f 5) 25
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
Remainder: Evaluation rule • Reduce step: Evaluate all sub-expressions (in any order) If the procedure to apply is a primitive, just do it. (+ (* 5 2) (* 5 4)) • 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) What is the order of the application of step 1 and step 2? (f (* 5 2))
Normal vs. Applicative order (define (f x) (+ x 1)) • Usually same value if expressions have no side effects • Consider ((lambda(x y) (+ x 2)) 1 (/ 1 0))
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)?
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*: Applicative Order !! *works for Beginning Student Language level only
Reminder: Special Forms Evaluation not according to standard rule or/and may have side effects
דוגמא – ערך מוחלט (define (abs x) (if (< x 0) (- x) x))
עוד דוגמא (define (foo a b) ((if (> b 0) + -) a b)) (foo 2 6) (foo 5 -4)
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) • 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) 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.
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)
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) (+ xy)) x is bounded to foo, while y is free
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
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.
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
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) • 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) 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.